Reputation: 1113
I want to use a convolutional neural network on a vector instead of a matrix. My input data is a list of 70000 string x_train
and their corresponding labels y_train
.
Here is the first 5 examples of x_train and y_train:
["Honestly, Buffalo is the correct answer. I remember people (somewhat) joking that Buffalo's mantra for starting goalies was "win a game, get traded".",
"I think Edmonton's front office was a travesty for the better part of 10 years, but Buffalo's systematic destruction of the term 'competitive' was much more responsible for the change to the draft lottery." ,
"Ah yes way could have been :( remember when he was drafted I thought he was gonna be great but nope could have had kawhi Thompson or jimmy butler
https://youtu.be/6xxbBR8iSZ0?t=40m49s If you didn't find it already.",
"Nothing out of the ordinary though, she just has eye constant eye contact.",
"He wouldn't have been a bad signing if we wouldn't have paid 18M euros. For the right price he would have been acceptable. "]
So I changed the strings into character array as follow:
x_train_array=[[]]*len(x_train)
for i in range(len(x_train)):
temp=list(x_train[i])
temp+=['0']*(400 - len(temp))
temp= [ord(c) for c in temp]
x_train_array[i]= np.expand_dims(temp, axis=0)
So that each string is now a 1 by 400 vector.
I now divide the dataset into training and validation set:
epochs=20
batch_size=50
num_classes=20
x_train_split1=x_train_array[0:60000]
x_train_split2=x_train_array[60000:]
y_train_split1= y_train_neralnet[0:60000]
y_train_split2= y_train_neralnet[60000:]
Now I built a convolutional neural network as follow:
model = Sequential()
model.add(Conv2D(64, kernel_size=(1, 9),
activation='relu',
input_shape=(1,400,1)))
model.add(Conv2D(128, (1, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(1, 2)))
model.add(Dropout(0.20))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.46))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Nadam(),
metrics=['accuracy'])
model.fit(x_train_split1, y_train_split1,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_train_split2, y_train_split2))
score = model.evaluate(x_train_split2, y_train_split2, verbose=0)
Now when I tried to run the code, I got the following error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-3-63f2874efbe3> in <module>
65 epochs=epochs,
66 verbose=1,
---> 67 validation_data=(x_train_split2, y_train_split2))
68 score = model.evaluate(x_train_split2, y_train_split2, verbose=0)
69 print('Test loss:', score[0])
/opt/conda/lib/python3.6/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, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
1152 sample_weight=sample_weight,
1153 class_weight=class_weight,
-> 1154 batch_size=batch_size)
1155
1156 # Prepare validation data.
/opt/conda/lib/python3.6/site-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
577 feed_input_shapes,
578 check_batch_axis=False, # Don't enforce the batch size.
--> 579 exception_prefix='input')
580
581 if y is not None:
/opt/conda/lib/python3.6/site-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
107 'Expected to see ' + str(len(names)) + ' array(s), '
108 'but instead got the following list of ' +
--> 109 str(len(data)) + ' arrays: ' + str(data)[:200] + '...')
110 elif len(names) > 1:
111 raise ValueError(
ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 60000 arrays: [array([[ 72, 111, 110, 101, 115, 116, 108, 121, 44, 32, 66, 117, 102,
102, 97, 108, 111, 32, 105, 115, 32, 116, 104, 101, 32, 99,
111, 114, 114, 101, 99, 116, 32, 97, 110, ...
Upvotes: 1
Views: 1695