使用tensorflow2时训练epoch出现
Epoch 35/100
54/55 [============================>.] - ETA: 0s - loss: 0.5587 - accuracy: 0.70622020-01-07 15:36:38.360506: W tensorflow/core/common_runtime/base_collective_executor.cc:216] BaseCollectiveExecutor::StartAbort Out of range: End of sequence
[[{{node IteratorGetNext}}]]
[[Shape/_6]]
问题,出现问题的代码为:
...
...#导入数据
db = tf.data.Dataset.from_tensor_slices((x_train, y_train))
db = db.shuffle(6000).batch(batchsz)
ds_val = tf.data.Dataset.from_tensor_slices((x_val, y_val))
ds_val = ds_val.batch(batchsz)
db_test = tf.data.Dataset.from_tensor_slices((x_test, y_test))
db_test = db_test.batch(batchsz)
network = Sequential([layers.Conv1D(32, kernel_size=6,strides=3,padding='same',activation=tf.nn.relu),
layers.Dropout(0.5),
layers.Conv1D(64, kernel_size=6, strides=3,padding='same',activation=tf.nn.relu),
layers.Dropout(0.5),
layers.Conv1D(128, kernel_size=6, strides=3,padding='same',activation=tf.nn.relu),
layers.Dropout(0.5),
layers.Flatten(),
layers.Dense(64, activation=tf.nn.relu),
layers.Dropout(0.4),
layers.Dense(1,activation='sigmoid')])
network.build(input_shape=(None, 2000,4))
network.summary()
network.compile(optimizer=optimizers.Adam(lr=0.001),
loss=tf.keras.losses.BinaryCrossentropy(),
metrics=['accuracy'] )
network.fit(db, epochs=1000, validation_data=ds_val,
validation_steps=2)
这是因为epoch在循环时,发现最后一个step数据不够一个batchsz,导致out-of-range错误,因此,需要在network.fit的时候加上steps_per_epoches的数据,例如如果你有1300个数据,batchsize=200,那么最后steps数目就为1300/200取整出6,然后在datasets中加repeat。
修改后的代码为:
db = tf.data.Dataset.from_tensor_slices((x_train, y_train))
db = db.shuffle(6000).batch(batchsz).repeat() # 加 repeat
ds_val = tf.data.Dataset.from_tensor_slices((x_val, y_val))
ds_val = ds_val.batch(batchsz)
db_test = tf.data.Dataset.from_tensor_slices((x_test, y_test))
db_test = db_test.batch(batchsz)
network = Sequential([layers.Conv1D(32, kernel_size=6, strides=3,padding='same',activation=tf.nn.relu),
layers.Dropout(0.5),
layers.Conv1D(64, kernel_size=6, strides=3,padding='same',activation=tf.nn.relu),
layers.Dropout(0.5),
layers.Conv1D(128, kernel_size=6, strides=3,padding='same',activation=tf.nn.relu),
layers.Dropout(0.5),
layers.Flatten(),
layers.Dense(64, activation=tf.nn.relu),
layers.Dropout(0.4),
layers.Dense(1,activation='sigmoid')])
network.build(input_shape=(None, 2000,4))
network.summary()
network.compile(optimizer=optimizers.Adam(lr=0.001),
loss=tf.keras.losses.BinaryCrossentropy(),
metrics=['accuracy']
)
network.fit(db, epochs=1000, validation_data=ds_val,
validation_steps=2,steps_per_epoch=x_train.shape[0]//batchsz)# 取整数steps的数目