鱼C论坛

 找回密码
 立即注册
查看: 2056|回复: 2

急急急 各位大佬帮我看看map函数

[复制链接]
发表于 2020-8-15 15:32:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我想请问一下在TensorFlow的dataset模块中有个map函数功能,我看论坛上都是直接调用编写好的函数,可是我在map之前已经写好了一个函数,为什么会提示没有函数的呢?

以下是我的代码,运行后报错了这个
TypeError: map() missing 1 required positional argument: 'map_func'
import tensorflow as tf
import numpy as np
import os
import gzip


def load_data(data_folder):

  files = [
      'train-labels-idx1-ubyte.gz', 'train-images-idx3-ubyte.gz',
      't10k-labels-idx1-ubyte.gz', 't10k-images-idx3-ubyte.gz'
  ]

  paths = []
  for fname in files:
    paths.append(os.path.join(data_folder,fname))

  with gzip.open(paths[0], 'rb') as lbpath:
    y_train = np.frombuffer(lbpath.read(), np.uint8, offset=8)

  with gzip.open(paths[1], 'rb') as imgpath:
    x_train = np.frombuffer(
        imgpath.read(), np.uint8, offset=16).reshape(len(y_train), 28, 28)

  with gzip.open(paths[2], 'rb') as lbpath:
    y_test = np.frombuffer(lbpath.read(), np.uint8, offset=8)

  with gzip.open(paths[3], 'rb') as imgpath:
    x_test = np.frombuffer(
        imgpath.read(), np.uint8, offset=16).reshape(len(y_test), 28, 28)

  return (x_train, y_train), (x_test, y_test)

(train_X,trian_label),(test_X,test_label) = load_data('C:\\Users\\Administrator\\Desktop\\data')


def func(x,y):
        x = np.expand_dims(x.astype(np.float32) / 255.0,axis = -1)
        return x,y

dataset = tf.data.Dataset.map(func)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-8-15 15:42:35 | 显示全部楼层
import tensorflow as tf
import numpy as np
import os
import gzip


def load_data(data_folder):
    files = [
        'train-labels-idx1-ubyte.gz', 'train-images-idx3-ubyte.gz',
        't10k-labels-idx1-ubyte.gz', 't10k-images-idx3-ubyte.gz'
    ]

    paths = []
    for fname in files:
        paths.append(os.path.join(data_folder, fname))

    with gzip.open(paths[0], 'rb') as lbpath:
        y_train = np.frombuffer(lbpath.read(), np.uint8, offset=8)

    with gzip.open(paths[1], 'rb') as imgpath:
        x_train = np.frombuffer(
            imgpath.read(), np.uint8, offset=16).reshape(len(y_train), 28, 28)

    with gzip.open(paths[2], 'rb') as lbpath:
        y_test = np.frombuffer(lbpath.read(), np.uint8, offset=8)

    with gzip.open(paths[3], 'rb') as imgpath:
        x_test = np.frombuffer(
            imgpath.read(), np.uint8, offset=16).reshape(len(y_test), 28, 28)

    return (x_train, y_train), (x_test, y_test)


(train_X, trian_label), (test_X, test_label) = load_data('C:\\Users\\Administrator\\Desktop\\data')


def func(x, y):
    x = np.expand_dims(x.astype(np.float32) / 255.0, axis=-1)
    return x, y


dataset = tf.data.Dataset().map(func)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-8-15 15:45:56 | 显示全部楼层

大佬好 可是还是报错呀
Can't instantiate abstract class DatasetV2 with abstract methods _inputs, element_spec
但是这次是报错这个
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-1-19 11:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表