我上岸啦 发表于 2020-8-15 15:32:34

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

我想请问一下在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, 'rb') as lbpath:
    y_train = np.frombuffer(lbpath.read(), np.uint8, offset=8)

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

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

with gzip.open(paths, '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)

zltzlt 发表于 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, 'rb') as lbpath:
      y_train = np.frombuffer(lbpath.read(), np.uint8, offset=8)

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

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

    with gzip.open(paths, '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)

我上岸啦 发表于 2020-8-15 15:45:56

zltzlt 发表于 2020-8-15 15:42


大佬好 可是还是报错呀
Can't instantiate abstract class DatasetV2 with abstract methods _inputs, element_spec
但是这次是报错这个
页: [1]
查看完整版本: 急急急 各位大佬帮我看看map函数