InvalidArgumentError when running model.fit() - python

When I run the below code:
model = Sequential()
model.add(Conv2D(64,(3,3),input_shape = X.shape[1:],data_format = "channels_last"))
model.add(Activation('relu'))
model.add(MaxPool2D(pool_size = (2,2)))
model.add(Conv2D(64,(3,3)))
model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(optimizer = 'adam',loss = 'binary_crossentropy',metrics = ['accuracy'])
model.fit(X, y, batch_size=32, epochs=1,
validation_split=0.3)
I'm getting:
InvalidArgumentError Traceback (most recent call
last) in ()
3 batch_size=32,
4 epochs=1,
----> 5 validation_split=0.3)
~\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py
in fit(self, x, y, batch_size, epochs, verbose, callbacks,
validation_split, validation_data, shuffle, class_weight,
sample_weight, initial_epoch, steps_per_epoch, validation_steps,
**kwargs) 1361 initial_epoch=initial_epoch, 1362 steps_per_epoch=steps_per_epoch,
-> 1363 validation_steps=validation_steps) 1364 1365 def evaluate(self,
~\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training_arrays.py
in fit_loop(model, inputs, targets, sample_weights, batch_size,
epochs, verbose, callbacks, val_inputs, val_targets,
val_sample_weights, shuffle, callback_metrics, initial_epoch,
steps_per_epoch, validation_steps)
262 ins_batch[i] = ins_batch[i].toarray()
263
--> 264 outs = f(ins_batch)
265 if not isinstance(outs, list):
266 outs = [outs]
~\Anaconda3\lib\site-packages\tensorflow\python\keras\backend.py in
call(self, inputs) 2910 feed_symbols != self._feed_symbols or self.fetches != self._fetches or 2911
session != self._session):
-> 2912 self._make_callable(feed_arrays, feed_symbols, symbol_vals, session) 2913 2914 fetched =
self._callable_fn(*array_vals)
~\Anaconda3\lib\site-packages\tensorflow\python\keras\backend.py in
_make_callable(self, feed_arrays, feed_symbols, symbol_vals, session) 2855 callable_opts.target.append(self.updates_op.name) 2856
Create callable.
-> 2857 callable_fn = session._make_callable_from_options(callable_opts) 2858 # Cache
parameters corresponding to the generated callable, so that 2859
we can detect future mismatches and refresh the callable.
~\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in
_make_callable_from_options(self, callable_options) 1412 """ 1413 self._extend_graph()
-> 1414 return BaseSession._Callable(self, callable_options) 1415 1416
~\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in
init(self, session, callable_options) 1366 with errors.raise_exception_on_not_ok_status() as status: 1367
self._handle = tf_session.TF_SessionMakeCallable(
-> 1368 session._session, options_ptr, status) 1369 finally: 1370 tf_session.TF_DeleteBuffer(options_ptr)
~\Anaconda3\lib\site-packages\tensorflow\python\framework\errors_impl.py
in exit(self, type_arg, value_arg, traceback_arg)
517 None, None,
518 compat.as_text(c_api.TF_Message(self.status.status)),
--> 519 c_api.TF_GetCode(self.status.status))
520 # Delete the underlying status object from memory otherwise it stays alive
521 # as there is a reference to status from this from the traceback due to
InvalidArgumentError: Default MaxPoolingOp only supports NHWC on
device type CPU [[Node: max_pooling2d_2/MaxPool =
MaxPoolT=DT_FLOAT,
_class=["loc:#training_3/Adam/gradients/max_pooling2d_2/MaxPool_grad/MaxPoolGrad"],
data_format="NCHW", ksize=[1, 1, 2, 2], padding="VALID", strides=[1,
1, 2, 2],
_device="/job:localhost/replica:0/task:0/device:CPU:0"]]
X.shape ----> (8000, 50, 50, 1)
Also I'm using 4GB RAM CPU. I've run only on one epoch. Does this have anything to do with the memory?

Related

Tensorflow: Blas GEMM launch failed: a.shape=(2, 128), b.shape=(128, 44), m=2, n=44, k=128

I am trying to run the classifier on the generated set of embedding. Error is as follows:
Epoch 1/2
---------------------------------------------------------------------------
InternalError Traceback (most recent call last)
<ipython-input-11-d859ddc5fde5> in <module>
2 y = np.zeros((2, ))
3
----> 4 model.fit(x, y, epochs=2)
~/.virtualenvs/pan_demo/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, max_queue_size, workers, use_multiprocessing, **kwargs)
878 initial_epoch=initial_epoch,
879 steps_per_epoch=steps_per_epoch,
--> 880 validation_steps=validation_steps)
881
882 def evaluate(self,
~/.virtualenvs/pan_demo/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_arrays.py in model_iteration(model, inputs, targets, sample_weights, batch_size, epochs, verbose, callbacks, val_inputs, val_targets, val_sample_weights, shuffle, initial_epoch, steps_per_epoch, validation_steps, mode, validation_in_fit, **kwargs)
327
328 # Get outputs.
--> 329 batch_outs = f(ins_batch)
330 if not isinstance(batch_outs, list):
331 batch_outs = [batch_outs]
~/.virtualenvs/pan_demo/lib/python3.6/site-packages/tensorflow/python/keras/backend.py in __call__(self, inputs)
3074
3075 fetched = self._callable_fn(*array_vals,
-> 3076 run_metadata=self.run_metadata)
3077 self._call_fetch_callbacks(fetched[-len(self._fetches):])
3078 return nest.pack_sequence_as(self._outputs_structure,
~/.virtualenvs/pan_demo/lib/python3.6/site-packages/tensorflow/python/client/session.py in __call__(self, *args, **kwargs)
1437 ret = tf_session.TF_SessionRunCallable(
1438 self._session._session, self._handle, args, status,
-> 1439 run_metadata_ptr)
1440 if run_metadata:
1441 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
~/.virtualenvs/pan_demo/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
526 None, None,
527 compat.as_text(c_api.TF_Message(self.status.status)),
--> 528 c_api.TF_GetCode(self.status.status))
529 # Delete the underlying status object from memory otherwise it stays alive
530 # as there is a reference to status from this from the traceback due to
InternalError: Blas GEMM launch failed : a.shape=(2, 128), b.shape=(128, 44), m=2, n=44, k=128
[[{{node layer_1/MatMul}}]]
[[{{node loss_1/output_loss/broadcast_weights/assert_broadcastable/is_valid_shape/has_valid_nonscalar_shape/has_invalid_dims/concat}}]]
Model looks like this:
model = tf.keras.Sequential([
tf.keras.layers.Dense(44, activation=tf.nn.relu, name='layer_1', input_shape=(128, )),
# tf.keras.layers.Dropout(0.3, name='layer_2'),
tf.keras.layers.Dense(num_of_employees, activation=tf.nn.softmax, name='output')
])
opt = tf.keras.optimizers.SGD(lr=0.34)
model.compile(optimizer=opt, loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.build()
x = np.zeros((2, 128))
y = np.zeros((2, ))
model.fit(x, y, epochs=2)
Also, it would be really handy if one can guide how to read the last part of stack trace, where it mentions node loss_1/output_loss...
short answer:
config = tensorflow.ConfigProto()
config.gpu_options.allow_growth = True
print('############## Allowing Growth ###########')
session = tf.Session(config=config)
# ------------------- start importing keras module ---------------------
import keras.backend.tensorflow_backend as K
from keras.backend.tensorflow_backend import set_session
set_session(tf.Session(config=config))
as mentioned here.
Jupyter Notebook's log suggested that failed to create cublas handle: CUBLAS_STATUS_NOT_INITIALIZED, for which above fix was required.

Including TensorBoard as a callback in Keras model fitting causes a FailedPreconditionError

Including tensorboard as callbacks in this code outputs an error:-
window_sizes=[3,5]
conv_layers=[1,2]
dense_layers=[1]
for ws in window_sizes:
for cl in conv_layers:
for dl in dense_layers:
name="{}-conv_layers-{}-window_size-{}-dense_layers-{}".format(cl,ws,dl,int(time.time()))
#keras.backend.clear_session()
model=Sequential()
model.add(Conv2D(64,(ws,ws),input_shape=X.shape[1:]))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(3,3)))
for i in range(cl-1):
model.add(Conv2D(64,(ws,ws)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(3,3)))
model.add(Flatten())
for i in range(dl):
model.add(Dense(64))
model.add(Activation("relu"))
model.add(Dropout(0.2))
model.add(Dense(1))
model.add(Activation("sigmoid"))
tensorboard = TensorBoard(log_dir="logs/{}".format(name))
model.compile(loss="binary_crossentropy",optimizer="adam",metrics=["accuracy"])
model.fit(X,y,batch_size=32,epochs=3,validation_split=0.2,callbacks=[tensorboard])
However, removing tensorboard from callbacks list fixed the error.
I tried resolving the error with tensorboard included in callbacks list , but nothing seemed to fix it. Here is the error:-
1-conv_layers-3-window_size-1-dense_layers-1546041626
Train on 19302 samples, validate on 4826 samples
Epoch 1/3
---------------------------------------------------------------------------
FailedPreconditionError Traceback (most recent call last)
<ipython-input-10-f65d5bcffb55> in <module>()
25 tensorboard = TensorBoard(log_dir="logs/{}".format(name))
26 model.compile(loss="binary_crossentropy",optimizer="adam",metrics=["accuracy"])
---> 27 model.fit(X,y,batch_size=32,epochs=3,validation_split=0.2,callbacks=[tensorboard])
28 #1-conv_layers-3-window_size-1-dense_layers-1546011295----98.59
29 #1-conv_layers-3-window_size-2-dense_layers-1546011765----98.15
~\Anaconda3\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
1037 initial_epoch=initial_epoch,
1038 steps_per_epoch=steps_per_epoch,
-> 1039 validation_steps=validation_steps)
1040
1041 def evaluate(self, x=None, y=None,
~\Anaconda3\lib\site-packages\keras\engine\training_arrays.py in fit_loop(model, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch, steps_per_epoch, validation_steps)
197 ins_batch[i] = ins_batch[i].toarray()
198
--> 199 outs = f(ins_batch)
200 outs = to_list(outs)
201 for l, o in zip(out_labels, outs):
~\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py in __call__(self, inputs)
2713 return self._legacy_call(inputs)
2714
-> 2715 return self._call(inputs)
2716 else:
2717 if py_any(is_tensor(x) for x in inputs):
~\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py in _call(self, inputs)
2673 fetched = self._callable_fn(*array_vals, run_metadata=self.run_metadata)
2674 else:
-> 2675 fetched = self._callable_fn(*array_vals)
2676 return fetched[:len(self.outputs)]
2677
~\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in __call__(self, *args, **kwargs)
1380 ret = tf_session.TF_SessionRunCallable(
1381 self._session._session, self._handle, args, status,
-> 1382 run_metadata_ptr)
1383 if run_metadata:
1384 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
~\Anaconda3\lib\site-packages\tensorflow\python\framework\errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
517 None, None,
518 compat.as_text(c_api.TF_Message(self.status.status)),
--> 519 c_api.TF_GetCode(self.status.status))
520 # Delete the underlying status object from memory otherwise it stays alive
521 # as there is a reference to status from this from the traceback due to
FailedPreconditionError: Attempting to use uninitialized value training_10/Adam/Variable_9
[[Node: training_10/Adam/Variable_9/read = Identity[T=DT_FLOAT, _class=["loc:#training_10/Adam/Assign_10"], _device="/job:localhost/replica:0/task:0/device:GPU:0"](training_10/Adam/Variable_9)]]
[[Node: metrics_10/acc/Mean_1/_1111 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_670_metrics_10/acc/Mean_1", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
Someone suggested adding keras.backend.clear_session() before creating the model, but even that did not help.
Using this
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten,Conv2D, MaxPooling2D,Activation
instead of
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten,Conv2D, MaxPooling2D,Activation
fixed it for me

Incompatible shapes error while training variational autoencoder

I am trying to train variational encoder. But I am getting
InvalidArgumentError: Incompatible shapes: [32,784] vs. [32,2352]
[[{{node custom_variational_layer_21/logistic_loss/mul}}]].
I read the images using opencv and append it to the list and then I converted it into numpy array.
Copied code from : http://www.stokastik.in/understanding-variational-autoencoders/
I am using convolutional variational autoencoder.
images = []
files = glob.glob('../dataset/maggi/*.*')
i=0
for file in files:
try:
img = cv2.imread(file)
img = cv2.resize(img, (28,28))
images.append(img)
except:
print('error')
x_train = np.asarray(images)
x_train = x_train.astype('float32') / 255.
print('Input size : ',x_train.shape)
conv_variational_autoencoder(x_train)
Output :
Input size : (1446, 28, 28, 3)
Epoch 1/50
----------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-166-2e8711de7bdc> in <module>()
72 print('Input size : ',x_train.shape)
73
---> 74 conv_variational_autoencoder(x_train)
<ipython-input-166-2e8711de7bdc> in conv_variational_autoencoder(X_train)
50 adam = Adam(lr=0.0005)
51 autoencoder.compile(optimizer=adam, loss=None)
---> 52 autoencoder.fit(X_train, shuffle=True, epochs=50, batch_size=32)
53
54
/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
1037 initial_epoch=initial_epoch,
1038 steps_per_epoch=steps_per_epoch,
-> 1039 validation_steps=validation_steps)
1040
1041 def evaluate(self, x=None, y=None,
/usr/local/lib/python3.6/dist-packages/keras/engine/training_arrays.py in fit_loop(model, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch, steps_per_epoch, validation_steps)
197 ins_batch[i] = ins_batch[i].toarray()
198
--> 199 outs = f(ins_batch)
200 outs = to_list(outs)
201 for l, o in zip(out_labels, outs):
/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in __call__(self, inputs)
2713 return self._legacy_call(inputs)
2714
-> 2715 return self._call(inputs)
2716 else:
2717 if py_any(is_tensor(x) for x in inputs):
/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in _call(self, inputs)
2673 fetched = self._callable_fn(*array_vals, run_metadata=self.run_metadata)
2674 else:
-> 2675 fetched = self._callable_fn(*array_vals)
2676 return fetched[:len(self.outputs)]
2677
/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in __call__(self, *args, **kwargs)
1437 ret = tf_session.TF_SessionRunCallable(
1438 self._session._session, self._handle, args, status,
-> 1439 run_metadata_ptr)
1440 if run_metadata:
1441 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
526 None, None,
527 compat.as_text(c_api.TF_Message(self.status.status)),
--> 528 c_api.TF_GetCode(self.status.status))
529 # Delete the underlying status object from memory otherwise it stays alive
530 # as there is a reference to status from this from the traceback due to
InvalidArgumentError: Incompatible shapes: [32,784] vs. [32,2352]
[[{{node custom_variational_layer_21/logistic_loss/mul}}]]
Thanks for the link to the article! It's a really interesting and good writeup.
Now for the problem:
As a rule: ALWAYS check your models' inputs and ouputs by using the model.summaray() function. In your case your model looks like this:
Now watch closely. Your input images are of the shape 28x28x3 like you defined yourself. But the output is 28x28x1 because the article you used trains the model on mnist, which is greyscale and thus only has 1 channel for colors, you have three.
This yields an error in the loss function, because it tries to compare how well a greyscale image looks like a color image, which of course doesn't work.
To fix this, all you have to do is go to the decoder part of the conv_variational_autoencoder(x_train) function and change the output size of the last Conv2DTranspose to be 28x28x3 instead of 28x28x1:
#Decoder
decoder_input = Input(shape=(196,))
p = Reshape((14, 14, 1))(decoder_input)
x = Conv2DTranspose(32, (3, 3), activation='relu', padding='same')(p)
x = UpSampling2D((2, 2))(x)
# dec_out = Conv2DTranspose(1, (3, 3), activation='sigmoid', padding='same')(x)
# Change the above line to:
dec_out = Conv2DTranspose(3, (3, 3), activation='sigmoid', padding='same')(x)
decoder = Model(decoder_input, dec_out)
And it should train straight away. Good luck!

InvalidArgumentError while training long short term memory network with Python/Keras (Audio classification)

I am new to Keras and I am trying to build a recurrent neural network to classify Audio files.
During the training, I am receiving an InvalidArgumentError: indices[28,0] = -711 is not in [0, 20000).
I have found various topics talking about this error but, to be honest, I did not understand what I have to change in the parameters I am passing to the network in order to help it managing the negative values I have in the training array.
Below the code:
from keras.models import Sequential
from keras.layers import Dense, Dropout, Embedding, LSTM, Bidirectional
max_features = 20000
maxlen = 40
batch_size = 32
model = Sequential()
model.add(Embedding(max_features, 128, input_length=maxlen))
model.add(Bidirectional(LSTM(64)))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
# try using different optimizers and different optimizer configs
model.compile('adam', 'binary_crossentropy', metrics=['accuracy'])
print('Train...')
model.fit(X_train, y_train,
batch_size=batch_size,
epochs=4,
validation_data=[X_test, y_test])
Error below:
Train...
Train on 964 samples, validate on 476 samples
Epoch 1/4
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-22-3452d23cb8b5> in <module>()
12 batch_size=batch_size,
13 epochs=4,
---> 14 validation_data=[X_test, y_test])
/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
1037 initial_epoch=initial_epoch,
1038 steps_per_epoch=steps_per_epoch,
-> 1039 validation_steps=validation_steps)
1040
1041 def evaluate(self, x=None, y=None,
/usr/local/lib/python3.6/dist-packages/keras/engine/training_arrays.py in fit_loop(model, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch, steps_per_epoch, validation_steps)
197 ins_batch[i] = ins_batch[i].toarray()
198
--> 199 outs = f(ins_batch)
200 outs = to_list(outs)
201 for l, o in zip(out_labels, outs):
/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in __call__(self, inputs)
2713 return self._legacy_call(inputs)
2714
-> 2715 return self._call(inputs)
2716 else:
2717 if py_any(is_tensor(x) for x in inputs):
/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in _call(self, inputs)
2673 fetched = self._callable_fn(*array_vals, run_metadata=self.run_metadata)
2674 else:
-> 2675 fetched = self._callable_fn(*array_vals)
2676 return fetched[:len(self.outputs)]
2677
/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in __call__(self, *args, **kwargs)
1437 ret = tf_session.TF_SessionRunCallable(
1438 self._session._session, self._handle, args, status,
-> 1439 run_metadata_ptr)
1440 if run_metadata:
1441 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
526 None, None,
527 compat.as_text(c_api.TF_Message(self.status.status)),
--> 528 c_api.TF_GetCode(self.status.status))
529 # Delete the underlying status object from memory otherwise it stays alive
530 # as there is a reference to status from this from the traceback due to
InvalidArgumentError: indices[28,0] = -711 is not in [0, 20000)
[[{{node embedding_3/embedding_lookup}} = GatherV2[Taxis=DT_INT32, Tindices=DT_INT32, Tparams=DT_FLOAT, _class=["loc:#training_1/Adam/Assign_2"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](embedding_3/embeddings/read, embedding_3/Cast, training_1/Adam/gradients/embedding_3/embedding_lookup_grad/concat/axis)]]
EDIT1: X_train is the float32 array below
array([[-5.79938449e+02, 6.63875936e+01, -6.75054944e+00, ...,
-2.89458464e+00, -2.30009868e+00, -2.34216322e+00],
[-3.38924973e+02, 1.60668197e+01, -5.39871140e+01, ...,
1.27180395e+00, 4.28090614e+00, 2.01538667e+00],
[-5.53199739e+02, 3.45314936e+01, -1.68711443e+01, ...,
-9.47345310e-02, -1.04780706e-02, 1.69060756e-01],
...,
[-5.91902354e+02, 6.14329122e+01, 1.43761675e+00, ...,
-4.38644438e+00, -3.67977820e+00, -1.89899207e+00],
[-7.04889969e+02, 6.24931510e+01, 1.90338300e+01, ...,
-1.47540089e+00, -1.75498741e+00, -4.55713837e-01],
[-8.24296641e+02, 7.43124586e+01, 1.43319513e+01, ...,
-7.60749297e-01, -1.05324700e+00, -8.54044186e-01]])

Keras Neural Network Error: Setting an Array Element with a Sequence

I'm loading dummy data into a neural network, but I'm receiving an error I can't seem to debug:
Here is my data, visualized:
df:
Label Mar
0 | [[.332, .326], [.058, .138]]
0 | [[.234, .246], [.234, .395]]
1 | [[.084, .23], [.745, .923]],
I'm trying to use the 'Mar' column to predict the 'Label' column (I know this data makes no sense, its just similar to my real data). Here is my neural network code:
model = Sequential()
model.add(Dense(3, input_dim=(1), activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
X = df['Mar']
Y = pd.get_dummies(df['Label'])
model.fit(X, Y, epochs=150, batch_size=10)
Here is the code to create my sample data:
Sample = [{'Label': 0, 'Mar': [[.332, .326], [.058, .138]]},
{'Label': 0, 'Mar': [[.234, .246], [.013, .592]]},
{'Label': 1, 'Mar': [[.084, .23], [.745, .923]]}]
df = pd.DataFrame(Sample)
When I get to the final row of this code, I get this error:
Epoch 1/150
-----------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-271-3d2506918d89> in <module>()
----> 1 model.fit(X, Y, epochs=150, batch_size=10)
/usr/local/lib/python2.7/site-packages/keras/models.pyc in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, **kwargs)
854 class_weight=class_weight,
855 sample_weight=sample_weight,
--> 856 initial_epoch=initial_epoch)
857
858 def evaluate(self, x, y, batch_size=32, verbose=1,
/usr/local/lib/python2.7/site-packages/keras/engine/training.pyc in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, **kwargs)
1496 val_f=val_f, val_ins=val_ins, shuffle=shuffle,
1497 callback_metrics=callback_metrics,
-> 1498 initial_epoch=initial_epoch)
1499
1500 def evaluate(self, x, y, batch_size=32, verbose=1, sample_weight=None):
/usr/local/lib/python2.7/site-packages/keras/engine/training.pyc in _fit_loop(self, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch)
1150 batch_logs['size'] = len(batch_ids)
1151 callbacks.on_batch_begin(batch_index, batch_logs)
-> 1152 outs = f(ins_batch)
1153 if not isinstance(outs, list):
1154 outs = [outs]
/usr/local/lib/python2.7/site-packages/keras/backend/tensorflow_backend.pyc in __call__(self, inputs)
2227 session = get_session()
2228 updated = session.run(self.outputs + [self.updates_op],
-> 2229 feed_dict=feed_dict)
2230 return updated[:len(self.outputs)]
2231
/usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in run(self, fetches, feed_dict, options, run_metadata)
776 try:
777 result = self._run(None, fetches, feed_dict, options_ptr,
--> 778 run_metadata_ptr)
779 if run_metadata:
780 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
/usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _run(self, handle, fetches, feed_dict, options, run_metadata)
952 np_val = subfeed_val.to_numpy_array()
953 else:
--> 954 np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)
955
956 if (not is_tensor_handle_feed and
/usr/local/lib/python2.7/site-packages/numpy/core/numeric.pyc in asarray(a, dtype, order)
529
530 """
--> 531 return array(a, dtype, copy=False, order=order)
532
533
ValueError: setting an array element with a sequence.
I now suspect it has something to do with my input columns being list, not np arrays? However, I've tried making them into arrays first and I'm still getting the same error. Would really love and appreciate help!!
Edit I've tried one hot encoding the label field, as I found somewhere online that that may help. It hasn't helped at this point
A couple of issues here,
The input is the wrong shape
The input is a mixture of arrays and lists.
One possible solution would be to use keras.layers.Flatten to reshape your data, and pd.Series.tolist() to uniformize the data type of the input array:
model = Sequential()
model.add(Flatten(input_shape=(2,2)))
model.add(Dense(3, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
X = df['Mar'].tolist()
Y = df['Label']
model.fit(X, Y, epochs=150, batch_size=10)

Categories

Resources