Mohammad Athar
Mohammad Athar

Reputation: 1980

what should tensorflow, lstm shape be when doing sequential models?

I keep getting shape errors with my data. I want to train an lstm to predict a sine wave, so i generate data

x_size = 100
xxx = [np.sin(5*np.pi*i/x_size)+.1*np.random.rand() for i in range(x_size)]
xxx = np.array(xxx)

This is one set of 100 samples, each being one-dimensional. So each epoch will have 100 data points in it (not worried about batch size yet, since it's small, but I want to do batch training eventually)

and then try to predict it

model = tf.keras.Sequential()
model.add(layers.LSTM(128, activation='relu',))
model.add(layers.Dense(1, activation='relu'))

model.compile(loss='mean_squared_error', 
              optimizer='sgd',
              metrics=['accuracy'])

model.fit(xxx, xxx)

But I can't get it to run the fit step. I've tried reshaping xxx in different ways, but nothing seems to work.

Is there something I'm missing?

Upvotes: 0

Views: 40

Answers (1)

monkeyking9528
monkeyking9528

Reputation: 126

I will use the comment in the rewrite example to show you your bugs

# it should have a sample size N and a feature size (M, 1)
# thus x has shape = (N, M, 1)
# y as label shape = (N, 1) for the output size of your dense layer is 1
# x_size = 100
N = 100
M = 1
xxx = np.random.rand(N, M, 1)
y = np.random.randint(0, 1, size = (N, 1))
# xxx = [np.sin(5*np.pi*i/x_size)+.1*np.random.rand() for i in range(x_size)]
# xxx = np.array(xxx)

model = Sequential()
model.add(LSTM(128, activation='relu',))
model.add(Dense(1, activation='relu'))

model.compile(loss='mean_squared_error', 
              optimizer='sgd',
              metrics=['accuracy'])
# if you choose accuracy as metric, output feature size is normally 1
model.fit(xxx, y)

Upvotes: 1

Related Questions