2
$\begingroup$

I apologize if this question is too elementary for this site. I am new in learning Keras and Tensorflow and I have the following type/shape problem on which I have already wasted too much time.

I entered this code (found on the web) to construct a keras model using sequential()

from keras.models import Sequential
from keras.layers import Dense, Activation

model = Sequential()
model.add(Dense(32, activation='relu', input_dim=100))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['accuracy'])

I then want to try the function model.evaluate(). But I can't find in the documentation nor in my trials and errors under what format the entry of evaluate should be. Among many other things, I have tried:

import numpy as np
model.evaluate(np.random.random((100,)))

but I get a long error message ending in

ValueError: Error when checking input: expected dense_1_input to have shape (100,) but got array with shape (1,)

Anyone has an idea what is happening here? Just a simple line of code creating a dummy entry that my model could evaluate() would unstuck me, I think.

$\endgroup$
1
  • $\begingroup$ Try model.evaluate(np.random.random((100, 3))) $\endgroup$ Commented Mar 30, 2019 at 19:41

1 Answer 1

2
$\begingroup$

model.evaluate requires both input and output, for example

evaluation = model.evaluate(np.random.random((1, 100)), np.random.random((1, 1)))

I think a step-by-step example would be more beneficial. Here is a working example:

from keras.models import Sequential
from keras.layers import Dense
import numpy as np

N = 1000
dimension = 100

# create some random input features (x) and output (y)
np.random.seed(0)
x = np.random.random((N, dimension))
y = np.random.random((N,))

# split the data into train and test sets
split = int(0.8 * N)
x_train = x[:split]
y_train = y[:split]
x_test = x[split:]
y_test = y[split:]

# build the model architecture
model = Sequential()
model.add(Dense(32, activation='relu', input_dim=dimension))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['accuracy'])

# train the model
model.fit(x_train, y_train, epochs=100)

# evaluate the model on train and test sets
train_loss = model.evaluate(x_train, y_train)[0]
test_loss = model.evaluate(x_test, y_test)[0]

print('train loss:', train_loss, ', test loss:', test_loss)

# predict (y) for a random input (x)
y_predict = model.predict(np.random.random((1, dimension)))
print('prediction:', y_predict)

which outputs binary_crossentropy loss:

train loss: 0.5500347983837127 , test loss: 0.7403841614723206
prediction: [[0.38731796]]

If you skip the training, i.e. commenting out

# train the model
model.fit(x_train, y_train, epochs=100)

the output will be

train loss: 0.7098221921920777 , test loss: 0.7191445398330688
prediction: [[0.32682237]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.