【发布时间】:2019-11-06 16:32:31
【问题描述】:
我正在构建一个自然网络,但我不了解 Conv1D 的输入维度。参数是批处理、步骤、通道,我使用的是 to_categorical,所以我的数据适合这个输入形状。我只是不确定我是否使用了正确的输入。它目前是批处理、功能、to_categorical 数组。这是正确的吗?
【问题讨论】:
标签: python machine-learning keras keras-layer
我正在构建一个自然网络,但我不了解 Conv1D 的输入维度。参数是批处理、步骤、通道,我使用的是 to_categorical,所以我的数据适合这个输入形状。我只是不确定我是否使用了正确的输入。它目前是批处理、功能、to_categorical 数组。这是正确的吗?
【问题讨论】:
标签: python machine-learning keras keras-layer
下面的示例代码有望阐明如何使用 Conv1D,以及尺寸的含义。提醒一下,在 Keras 中,通常在定义模型时不指定批次/样本维度。它是从实际输入数据中自动推断出来的。这就是为什么在定义 x_train 和 y_train 之前看不到“num_samples”的原因。我希望这会有所帮助。
import tensorflow as tf
import numpy as np
num_output_units = 4
num_time_steps = 10
num_features = 6
num_samples = 20
myInput = tf.keras.layers.Input(shape=(num_time_steps, num_features))
x = tf.keras.layers.Conv1D(num_output_units, kernel_size=3, padding='same')(myInput)
final_output = tf.keras.layers.Dense(1)(x)
myModel = tf.keras.Model(inputs=myInput, outputs=final_output)
# display the model architecture
print(myModel.summary())
# Input data
x_train = np.random.random((num_samples, num_time_steps, num_features))
# Target/label data
y_train = np.random.random((num_samples, num_time_steps, 1))
myModel.compile(optimizer='rmsprop', loss='mse')
# train the model
myModel.fit(x_train, y_train, epochs=2)
myModel.predict(x_train)
【讨论】: