LogicLover90
LogicLover90

Reputation: 229

keras, invalid predict size

im quite new in keras I have trained this with (100,8) size of input and output, i want to 1*8 output with 1*8 predict data. for example input that i enter 1*8. code returns, 1*8 output data.

here is my code:

from tensorflow import keras
import numpy as np
import tensorflow as tf

from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation 
import keras
from keras.layers import Input, Dense
accuracy = tf.keras.metrics.CategoricalAccuracy()
import numpy as np

xs=np.ones((100,8))
ys=np.ones((100,8))

for i in range(100):
    xs[i]*=np.random.randint(30, size=8)  
    ys[i]=xs[i]*2

xs=xs.reshape(1,100,8)   
ys=ys.reshape(1,100,8)   
# model = tf.keras.Sequential([layers.Dense(units=1, input_shape=[2,4])])
model = Sequential() 
model.add(Dense(10,input_shape=[100,8])) 


model.add(Activation('relu'))
model.add(Dropout(0.15))
model.add(Dense(10)) 
model.add(Activation('relu')) 
# model.add(Dropout(0.5))
model.add(Dense(8)) 

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

model.fit(xs, ys, epochs=1000, batch_size=100)

p= np.array([[1,3,4,5,9,2,3,4]]).reshape(1,1,8)

print(model.predict(p))

Upvotes: 2

Views: 39

Answers (1)

Marco Cerliani
Marco Cerliani

Reputation: 22021

you don't need to add one dimension in the first position of your data. for 2D network, you simply have to feed your model with data in the format (n_sample, n_features)

here the complete example

xs=np.ones((100,8))
ys=np.ones((100,8))

for i in range(100):
    xs[i]*=np.random.randint(30, size=8)  
    ys[i]=xs[i]*2

xs=xs.reshape(100,8)   
ys=ys.reshape(100,8)   

model = Sequential() 
model.add(Dense(10,input_shape=(8,))) 
model.add(Activation('relu'))
model.add(Dropout(0.15))
model.add(Dense(10)) 
model.add(Activation('relu')) 
model.add(Dense(8)) 

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

model.fit(xs, ys, epochs=10, batch_size=100)

p = np.array([[1,3,4,5,9,2,3,4]]) # (1, 8)

pred = model.predict(p)

print(pred)
print(pred.shape) # (1, 8)

Upvotes: 1

Related Questions