这是我的代码的简化版本,它会引发标题中提到的错误:
我将用于演示的是这个csv文件。
import tensorflow as tf
BATCH_SIZE = 3
SEQ_LENGTH = 4
NUM_CLASSES = 2
LSTM_UNITS = 64
NUM_SHARDS = 4
NUM_CHANNELS = 2
tf.enable_eager_execution()
def keras_model():
inputs = tf.keras.layers.Input(shape=(SEQ_LENGTH, NUM_CHANNELS))
x = tf.keras.layers.Bidirectional(
tf.keras.layers.LSTM(LSTM_UNITS, return_sequences=True))(inputs)
outputs = tf.keras.layers.TimeDistributed(tf.keras.layers.Dense(NUM_CLASSES, activation='relu'))(x)
return tf.keras.Model(inputs, outputs)
dataset = tf.data.experimental.CsvDataset(filenames='../../input/aFile.csv', header=True,record_defaults=[tf.int64] * 3, select_cols=[0,1,2])
dataset= dataset.window(size=SEQ_LENGTH, shift=1, drop_remainder=True).flat_map(lambda f1,f2, label:
tf.data.Dataset.zip((tf.data.Dataset.zip((f1.batch(SEQ_LENGTH),f2.batch(SEQ_LENGTH))), label.batch(SEQ_LENGTH))))
dataset = dataset.batch(BATCH_SIZE, drop_remainder=True)
train_iterator = dataset.make_one_shot_iterator()
train_features, train_labels = train_iterator.get_next()
print(train_features)
print(train_labels)
model = keras_model()
model.summary()
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])
model.fit(x=train_features,y=train_labels, batch_size=BATCH_SIZE,epochs=1, steps_per_epoch=10)
这是代码的输出结果:
...
(<tf.Tensor: id=44, shape=(3, 4), dtype=int64, numpy=
array([[0, 1, 2, 3],
[1, 2, 3, 4],
[2, 3, 4, 5]], dtype=int64)>, <tf.Tensor: id=45, shape=(3, 4), dtype=int64, numpy=
array([[100, 101, 102, 103],
[101, 102, 103, 104],
[102, 103, 104, 105]], dtype=int64)>)
tf.Tensor(
[[0 0 0 0]
[0 0 0 1]
[0 0 1 0]], shape=(3, 4), dtype=int64)
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) (None, 4, 2) 0
_________________________________________________________________
bidirectional (Bidirectional (None, 4, 128) 34304
_________________________________________________________________
time_distributed (TimeDistri (None, 4, 2) 258
=================================================================
Total params: 34,562
Trainable params: 34,562
Non-trainable params: 0
_________________________________________________________________
...
ValueError: Error when checking input: expected input_1 to have 3 dimensions, but got array with shape (3, 4)
Process finished with exit code 1
我将用于演示的是这个csv文件。
f1,f2,label
0,100,0
1,101,0
2,102,0
3,103,0
4,104,1
5,105,0
6,106,0
7,107,0
8,108,1
9,109,0
10,110,0
前两列是来自两个不同通道的特征列,最后一列包含标签。我需要使用例如四行数据的序列作为时间馈入模型,而批量大小例如为三,因此输入形状将类似于每个包含两个值的四行的三个批次。
我认为我需要使用一些重塑函数,但无法弄清楚如何操作。
请问有谁能告诉我如何解决这个问题?