Format of adding hidden layers in Keras.

Viewed 12712

I wrote a neural network code and I want to add hidden layers to it. I have access to this small part of code:

trainX, trainY = create_dataset(train, look_back)
testX, testY = create_dataset(test, look_back)

trainX = numpy.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
testX = numpy.reshape(testX, (testX.shape[0], 1, testX.shape[1]))

model = Sequential()
model.add(LSTM(4, input_shape=(1, look_back)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(trainX, trainY, epochs=100, batch_size=1, verbose=2)

trainPredict = model.predict(trainX)
testPredict = model.predict(testX)

Is there any way to add hidden layers to it with this much information available? Also, this code works fine in Python3.

It would be a great help. Thanks.

2 Answers

The mentioned code is the entire implementation of core ML part.

Here you created model,
model = Sequential()

This is input layer,
model.add(LSTM(4, input_shape=(1, look_back)))

This is output layer
model.add(Dense(1))

Model compilation
model.compile(loss='mean_squared_error', optimizer='adam')

Model training
model.fit(trainX, trainY, epochs=100, batch_size=1, verbose=2)

Any layer added between input and output layer is called Hidden layer, you can easily add and your final code will look like below,

trainX, trainY = create_dataset(train, look_back)
testX, testY = create_dataset(test, look_back)

trainX = numpy.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
testX = numpy.reshape(testX, (testX.shape[0], 1, testX.shape[1]))

model = Sequential()
model.add(LSTM(4, input_shape=(1, look_back)))
model.add(Dense(4)) # New hidden layer with 4 params
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(trainX, trainY, epochs=100, batch_size=1, verbose=2)

trainPredict = model.predict(trainX)
testPredict = model.predict(testX)

You can try adding hidden layers using the following format structure. The example is not applied to your problem, though:

from tensorflow.keras.layers import Dense
from tensorflow.keras import Model, Input

input_layer = Input(shape=(3,), name='input') # 3 dimensional input
hidden_layer1 = Dense(units=20, activation="sigmoid", name="hidden_layer1")(input_layer)
hidden_layer2 = Dense(units=20, activation="sigmoid", name="hidden_layer2")(hidden_layer1)
output_layer = Dense(units=1, activation="sigmoid", name="output_layer")(hidden_layer2)
  
# Create the model
model = Model(input_layer, output_layer)
Related