鱼C论坛

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

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

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

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

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

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

以下是我的代码,运行后报错了这个
TypeError: map() missing 1 required positional argument: 'map_func'

  1. import tensorflow as tf
  2. import numpy as np
  3. import os
  4. import gzip


  5. def load_data(data_folder):

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

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

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

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

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

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

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

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


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

  28. dataset = tf.data.Dataset.map(func)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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


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

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

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

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

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

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

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


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


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


  28. dataset = tf.data.Dataset().map(func)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

大佬好 可是还是报错呀
Can't instantiate abstract class DatasetV2 with abstract methods _inputs, element_spec
但是这次是报错这个
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-25 17:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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