如何在不使用输入的情况下定义输入形状?

时间:2020-07-28 02:22:42

标签: python tensorflow keras lstm

我正在接受培训,以弄清楚如何在模型创建过程中不使用X_train的情况下定义培训数据的输入形状。我知道LSTM网络仅接受3D阵列作为输入,并且我的数据已为此for循环准备

X_train = []
y_train = []
for i in range(10, 400):
    X_train.append(training_set_scaled[i-10:i, 0])
    y_train.append(training_set_scaled[i, 0])
X_train, y_train = np.array(X_train), np.array(y_train)
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))

下面是我正在使用的模型。

model = Sequential()
model.add(LSTM(units = 100, return_sequences = True, input_shape = (X_train.shape[1], 1)))
model.add(Dropout(0.2))

model.add(LSTM(units=1000 , return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=1000 , return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=100))
model.add(Dropout(0.2))
model.add(Dense(units=1 ))
model.compile(optimizer=opt, loss='mean_squared_error' , metrics=[tf.keras.metrics.RootMeanSquaredError()])

您可以看到第一LSTM层的输入形状中包含X_train,无需使用X_train就可以定义输入形状,它知道需要使用X_train进行训练,因为我们拥有{{ 1}},其中model.fit作为其参数之一

如果执行以下操作,我的模型将无法以相同的方式进行训练

X_train

1 个答案:

答案 0 :(得分:1)

您尝试使用None代替X_train吗?这样吗?

<Row className="marginBottom15"> <Row className="marginBottom10"> <Col>Store Hours:</Col> <Col className="marginLeft15"> <Checkbox.Group defaultValue={defaultDays} onChange={onCheckBoxChange}> <Checkbox value="monday">Monday</Checkbox> <Checkbox value="tuesday">Tuesday</Checkbox> <Checkbox value="wednesday">Wednesday</Checkbox> <Checkbox value="thursday">Thursday</Checkbox> <Checkbox value="friday">Friday</Checkbox> <Checkbox value="saturday">Saturday</Checkbox> <Checkbox value="sunday">Sunday</Checkbox> </Checkbox.Group> </Col> </Row> {defaultDays.map((day) => { return ( <Row className="alignBaseline flexWrapper marginTop5"> <Col className="width100px">{toTitleCase(day)}</Col> <Col className="marginLeft15 "> <Form.Item className="fullWidth" name={`${day}StartTime`} label="Start Time"> <TimePicker defaultValue={_commonTime.startTime} onChange={(time) => onTimeChange(time, `${day}StartTime`, `${day}`) } minuteStep={15} format={timeFormat} /> </Form.Item> </Col> <Col className="marginLeft30"> <Form.Item className="fullWidth" name={`${day}EndTime`} label="End Time"> <TimePicker defaultValue={_commonTime.endTime} onChange={(time) => onTimeChange(time, `${day}EndTime`, `${day}`) } minuteStep={15} format={timeFormat} /> </Form.Item> </Col> </Row> ); })} </Row> function onCheckBoxChange(checkedValues) { setDefaultDays(checkedValues); }