为了账号安全,请及时绑定邮箱和手机立即绑定

带有 tf 数据集输入的 Tensorflow keras

带有 tf 数据集输入的 Tensorflow keras

斯蒂芬大帝 2021-07-09 14:01:41
我是 tensorflow keras 和数据集的新手。谁能帮我理解为什么下面的代码不起作用?import tensorflow as tfimport tensorflow.keras as kerasimport numpy as npfrom tensorflow.python.data.ops import dataset_opsfrom tensorflow.python.data.ops import iterator_opsfrom tensorflow.python.keras.utils import multi_gpu_modelfrom tensorflow.python.keras import backend as Kdata = np.random.random((1000,32))labels = np.random.random((1000,10))dataset = tf.data.Dataset.from_tensor_slices((data,labels))print( dataset)print( dataset.output_types)print( dataset.output_shapes)dataset.batch(10)dataset.repeat(100)inputs = keras.Input(shape=(32,))  # Returns a placeholder tensor# A layer instance is callable on a tensor, and returns a tensor.x = keras.layers.Dense(64, activation='relu')(inputs)x = keras.layers.Dense(64, activation='relu')(x)predictions = keras.layers.Dense(10, activation='softmax')(x)# Instantiate the model given inputs and outputs.model = keras.Model(inputs=inputs, outputs=predictions)# The compile step specifies the training configuration.model.compile(optimizer=tf.train.RMSPropOptimizer(0.001),          loss='categorical_crossentropy',          metrics=['accuracy'])# Trains for 5 epochsmodel.fit(dataset, epochs=5, steps_per_epoch=100)
查看完整描述

2 回答

?
幕布斯7119047

TA贡献1794条经验 获得超8个赞

关于您为什么收到错误的原始问题:

Error when checking input: expected input_1 to have 2 dimensions, but got array with shape (32,)

您的代码中断的原因是因为您没有将.batch()back应用于dataset变量,如下所示:

dataset = dataset.batch(10)

您只需调用dataset.batch().

这会中断,因为没有batch()输出张量不会批量处理,即您得到的是 shape(32,)而不是(1,32).


查看完整回答
反对 回复 2021-07-13
  • 2 回答
  • 0 关注
  • 170 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号