I have the following code,
from keras.models import Sequential
from keras.layers import Dense
import numpy as np
# load dataset
dataset = np.loadtxt("data.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:, 0:8]
Y = dataset[:, 8]
# create model
model = Sequential()
model.add(Dense(8, activation="relu", input_dim=8, kernel_initializer="uniform"))
model.add(Dense(12, activation="relu", kernel_initializer="uniform"))
model.add(Dense(1, activation="sigmoid", kernel_initializer="uniform"))
# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(X, Y, epochs=150, batch_size=10, verbose=2)
# calculate predictions
test = np.array([6,148,72,35,0,33.6,0.627,50])
predictions = model.predict(test)
# round predictions
rounded = [round(x[0]) for x in predictions]
print(rounded)
When I run the program, it gives me the following error.
ValueError: Error when checking : expected dense_1_input to have
shape (None, 8) but got array with shape (8,1)
I know there are a lot of duplicates to this question, I have tried all of them but it still gives me the same errors. How do I solve it?
Eventhough we don't see the full error trace, I think that the model learns and the error comes at the line :
predictions = model.predict(test)
Please confirm this.
The prediction fails because what you should always feed the network with is a numpy array of shape (number_of_samples_to_predict, input_shape).
There is always an additionnal dimension at the beginning, this is where you pile up all of the samples that you want to predict. When there is only one sample, you still have to feed a [1, input_shape] array.
To fix this use define your test input like this :
test = np.array([[6,148,72,35,0,33.6,0.627,50]])
now test has shape (1,8) which should run as the model expects (?,8).
well i think that this is diabetes dataset, you will get desired output by doing
this
model.predict(np.array([[6,148,72,35,0,33.6,0.627,50]])) > 0.5
test = test.transpose
And now you will have test of desired shape.
Related
I am trying to train an 1D CNN to recognise bearing faults using the data from the WCRU. I am having difficulties while defining the input_shape an the first layer of my model. My 'train_X' is a vector with dimensions (60800,1). This is the code I use:
X_train = numpy.loadtxt('training_dataX.txt',dtype=float)
Y_train = numpy.loadtxt('training_dataY.txt',dtype=int)
X_test = numpy.loadtxt('testing_dataX.txt',dtype=float)
Y_test = numpy.loadtxt('testing_dataY.txt',dtype=int)
Y_train = np_utils.to_categorical(Y_train) #one hot encode outputs
Y_test = np_utils.to_categorical(Y_test)
num_classes = Y_test.shape[1]
e=0.01 #create a callback to monitor the error to avoid overfitting
class myCallback(keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
if(logs.get('val_loss') > e):
print("\nReached %2.2f%% error, so stopping training!!" %(e*100))
self.model.stop_training = True
def baseline_model(): #building our sequential model
model = Sequential()
model.add(Conv1D(60,9,activation='tanh',padding='same',input_shape=(1,1)))
model.add(MaxPooling1D(4))
model.add(Conv1D(40,9,activation='tanh',padding='same'))
model.add(MaxPooling1D(4))
model.add(Conv1D(40,9,activation='tanh',padding='same'))
model.add(Flatten())
model.add(Dense(20,activation='tanh'))
model.add(Dense(num_classes,activation='tanh'))
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
return model
model = baseline_model() #initialize fitting process
model.fit(X_train, Y_train, validation_data=(X_test,Y_test),epochs=100,batch_size=10,callbacks=['callbacks'])
scores = model.evaluate(X_test,Y_test,verbose=0) #final model evaluation
print('CNN Error: %.2f%%' % (100-scores[1]*100))
Ufortunately I am getting this error message i cant figure out the reason:
ValueError: The shape of the input to "Flatten" is not fully defined (got (0, 40)).
Make sure to pass a complete "input_shape" or "batch_input_shape" argument to the first layer in your model.
I 've tryied changing the input_shape to (1,) but the I get this error:
ValueError: Input 0 is incompatible with layer conv1d_24: expected ndim=3, found ndim=2
Any suggestions would be appreciated. Thank you in advance.
First of all, since you are doing classification, the final activation function should be softmax and not tanh. Second of all, if each example is a vector of dimension (60800, 1), passing that as the input shape is necessary. Check code below:
from tensorflow.keras.layers import Input, Convolution1D, MaxPooling1D, GlobalAveragePooling1D, UpSampling1D, Conv1D, Flatten, Dense
input_shape = (60800, 1)
num_classes = 10
model = Sequential()
model.add(Conv1D(60,9,activation='tanh',padding='same',input_shape=input_shape))
model.add(MaxPooling1D(4))
model.add(Conv1D(40,9,activation='tanh',padding='same'))
model.add(MaxPooling1D(4))
model.add(Conv1D(40,9,activation='tanh',padding='same'))
model.add(Flatten())
model.add(Dense(20,activation='tanh'))
model.add(Dense(num_classes,activation='softmax'))
model.summary()
EDITS
I used num_classes to be 10 because I do not know the number of classes, but you can change it accordingly.
I am using the mnist dataset(digits), and would like to implement mean squared error loss function, however I have the following error:
ValueError: A target array with shape (60000, 1) was passed for an output of shape (None, 10) while using as loss mean_squared_error. This loss expects targets to have the same shape as the output.
this is my code:
Originally, I tried sparse_categorical_crossentropy
Code modified from: https://www.youtube.com/watch?v=wQ8BIBpya2k
import tensorflow as tf
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test,y_test) = mnist.load_data()
x_train = tf.keras.utils.normalize(x_train, axis = 1)
x_test = tf.keras.utils.normalize(x_test, axis = 1)
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28,28)),
tf.keras.layers.Dense(128, activation='sigmoid'),
tf.keras.layers.Dense(10, activation='sigmoid')
])
model.compile(optimizer='SGD',
loss='mean_squared_error',
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=3)
How can I reshape my data so that it works with MSE?
I guess you missing something very important here. You are trying to use a metric used in regression (Mean-Squared-Error) for a classification task (predicting classes). These two objectives are different tasks in the machine learning world.
If like to try it anyway, just reshape your last layer to one output-neuron and ReLU-activation:
tf.keras.layers.Dense(1, activation='relu')
One output neuron and ReLU-activation since your label is just the (integer) numbers from 0 to 9. Sigmoid gives you continuous values between 0 and 1, so this won't bring you any success in this case.
Keep in mind your model doesn't do classification anymore, it will give you a continuous number between 0 and inf. So don't be surprised if you get e.g. 3.1415 as output if you feed an image of a 3 into your model. The model tries now to produce outputs as close as possible to the number in the label.
I'm trying to train a model on features i extracted from some images, model trains fine, but when i try model.predict it gives me this error.
" expected dense_input to have shape (7,) but got array with shape (1,)"
i have the knowledge about the shape of the input but the error is just weird. it makes no sense to me right now i tried to print the shape of the input i am giving to model.predict and its fine.
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation
trainX = np.array(train_set)
trainY = np.array(train_labels)
model = Sequential()
model.add(Dense(8, input_dim=7, activation='relu'))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(trainX, trainY, nb_epoch=1200, batch_size=2, verbose=2)
model.save('my_model.h5')
for i in np.array(test_set):
print(i.shape)
dataPrediction = model.predict(i)
print (dataPrediction, '<--- Predicted number')
print (test_labels[i],' <-- Correct answer \n')
print(i.shape) gives me (7,)
yet it gives me error
Error when checking input: expected dense_input to have shape (7,) but got array with shape (1,)
That's because your model expects an array of samples, but you're giving it a single sample at a time.
It therefore treated each feature in your sample as an individual sample of shape (1,), which didn't make sense to it since you presumably had 7 features and it therefore expected a sample of shape (7,).
You can just do model.predict(np.array(test_set)).
try
dataPrediction = model.predict(np.expand_dims(i,axis=0))
Note: I already read keras forward pass with tensorflow variable as input but it did not help.
I'm training an auto-encoder unsupervised neural-network with Keras with the MNIST database:
import keras, cv2
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(60000, 784).astype('float32') / 255.0
x_test = x_test.reshape(10000, 784).astype('float32') / 255.0
model = Sequential()
model.add(Dense(100, activation='sigmoid', input_shape=(784,)))
model.add(Dense(10, activation='sigmoid'))
model.add(Dense(100, activation='sigmoid'))
model.add(Dense(784, activation='sigmoid'))
model.compile(loss='mean_squared_error', optimizer='sgd')
history = model.fit(x_train, x_train, batch_size=1, epochs=1, verbose=0)
Then I would like to get the output vector when the input vector is x_test[i]:
for i in range(100):
x = x_test[i]
a = model(x)
cv2.imshow('img', a.reshape(28,28))
cv2.waitKey(0)
but I get this error:
All inputs to the layer should be tensors.
How should I modify this code to do a forward pass of an input vector in the neural network, and get a vector in return?
Also how to get the activation after, say, the 2nd layer? i.e. don't propagate until the last layer, but get the output after the 2nd layer.
Example: input: vector of size 784, output: vector of size 10
To run a model after you've finished training it you need to use keras predict(). This will evaluate the graph, given your input data. Note that the input data must be the same dimensions as the specified model inputs, which in your case looks to be [None, 784]. Keras does not require you to specify the batch dimension but you still need a 2D array going in. Do something like..
x = x_test[5]
x = x[numpy.newaxis,:]
out_val = model.predict(x)[0]
if you just want to process a single value.
The numpy.newaxis is required to make a 2D array and thus match your input size. You can skip this if you pass in an array of values to evaluate all at once.
With Keras/Tensorflow, your model is a graph/function, not standard python procedural code. You can't call it with data directly. You need to create functions and then call the functions. To get the output from an intermediate layer you can do something like..
OutFunc = K.function([model.input], [model.layers[2].output])
out_val = OutFunc([x])[0]
again, keep in mind there is a batch dimension on the input which will be produced in the output. There's a number of posts on getting data from intermediate layers if you need some additional examples. For instance see Keras, How to get the output of each layer?
An other way to do this than the accepted answer: when x is just a (784,) or (784,1) numpy array, we can use this:
model.predict([[x]])
with a double [[...]].
I am trying to predict the next value in the time series using the previous 20 values. Here is a sample from my code:
X_train.shape is (15015, 20)
Y_train.shape is (15015,)
EMB_SIZE = 1
HIDDEN_RNN = 3
model = Sequential()
model.add(LSTM(input_shape = (EMB_SIZE,), input_dim=EMB_SIZE, output_dim=HIDDEN_RNN, return_sequences=True))
model.add(LSTM(input_shape = (EMB_SIZE,), input_dim=EMB_SIZE, output_dim=HIDDEN_RNN, return_sequences=False))
model.add(Dense(1))
model.add(Activation('softmax'))
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
model.fit(X_train,
Y_train,
nb_epoch=5,
batch_size = 128,
verbose=1,
validation_split=0.1)
score = model.evaluate(X_test, Y_test, batch_size=128)
print score
Though when I ran my code I got the following error:
TypeError: ('Bad input argument to theano function with name "/usr/local/lib/python2.7/dist-packages/keras/backend/theano_backend.py:484" at index 0(0-based)', 'Wrong number of dimensions: expected 3, got 2 with shape (32, 20).')
I was trying to replicate the results in this post: neural networks for algorithmic trading. Here is a link to the git repo: link
It seems to be a conceptual error. Please post any sources where I can get a better understanding of LSTMS for time series prediction. Also please explain me how I fix this error, so that I can reproduce the results mentioned in the article mentioned above.
If I understand your problem correctly, your input data a set of 15015 1D sequences of length 20. According to Keras doc, the input is a 3D tensor with shape (nb_samples, timesteps, input_dim). In your case, the shape of X should then be (15015, 20, 1).
Also, you just need to give input_dim to the first LSTM layer. input_shape is redundant and the second layer will infer its input shape automatically:
model = Sequential()
model.add(LSTM(input_dim=EMB_SIZE, output_dim=HIDDEN_RNN, return_sequences=True))
model.add(LSTM(output_dim=HIDDEN_RNN, return_sequences=False))
LSTM in Keras has an input tensor shape of (nb_samples, timesteps, feature_dim)
In your case, X_train should probably have an input shape of (15015, 20, 1). Just reshape it accordingly and the model should run.