亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

帶有 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 關注
  • 191 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號