我正在使用keras构建简单的CNN。
这是我的代码-
from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.layers import Dense, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.models import Sequential
import matplotlib.pylab as plt
import numpy as np
import pandas as pd
batch_size = 128
num_classes = 2
epochs = 10
input_shape = (21,109714,1)
train = pd.read_csv('train_features.csv')
test = pd.read_csv('test_features.csv')
x_train = train.iloc[:, 1:20].values
y_train = train.iloc[:, 0].values
x_test = test.iloc[:,1:20].values
y_test = test.iloc[:,0].values
model = Sequential()
model.add(Conv2D(32, kernel_size=(5, 5), strides=(1, 1),
activation='relu',
input_shape=input_shape))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Conv2D(64, (5, 5), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(1000, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=None,
metrics=['accuracy'])
class AccuracyHistory(keras.callbacks.Callback):
def on_train_begin(self, logs={}):
self.acc = []
def on_epoch_end(self, batch, logs={}):
self.acc.append(logs.get('acc'))
history = AccuracyHistory()
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test),
callbacks=[history])
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
plt.plot(range(1, 11), history.acc)
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.show()
我得到的错误-
Traceback (most recent call last):
File "E:\anaconda\lib\site-packages\theano\compile\function_module.py", line 903, in __call__
self.fn() if output_subset is None else\
ValueError: rng_mrg cpu-implementation does not support more than (2**31 -1) samples
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "cnn.py", line 64, in <module>
model.add(Dense(1000, activation='relu'))
File "E:\anaconda\lib\site-packages\keras\engine\sequential.py", line 187, in add
output_tensor = layer(self.outputs[0])
File "E:\anaconda\lib\site-packages\keras\engine\base_layer.py", line 432, in __call__
self.build(input_shapes[0])
File "E:\anaconda\lib\site-packages\keras\layers\core.py", line 872, in build
constraint=self.kernel_constraint)
File "E:\anaconda\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "E:\anaconda\lib\site-packages\keras\engine\base_layer.py", line 252, in add_weight
constraint=constraint)
File "E:\anaconda\lib\site-packages\keras\backend\theano_backend.py", line 152, in variable
value = value.eval()
File "E:\anaconda\lib\site-packages\theano\gof\graph.py", line 525, in eval
rval = self._fn_cache[inputs](*args)
File "E:\anaconda\lib\site-packages\theano\compile\function_module.py", line 917, in __call__
storage_map=getattr(self.fn, 'storage_map', None))
File "E:\anaconda\lib\site-packages\theano\gof\link.py", line 325, in raise_with_op
reraise(exc_type, exc_value, exc_trace)
File "E:\anaconda\lib\site-packages\six.py", line 692, in reraise
raise value.with_traceback(tb)
File "E:\anaconda\lib\site-packages\theano\compile\function_module.py", line 903, in __call__
self.fn() if output_subset is None else\
ValueError: rng_mrg cpu-implementation does not support more than (2**31 -1) samples
Apply node that caused the error: mrg_uniform{TensorType(float32, matrix),inplace}(<TensorType(int32, matrix)>, TensorConstant{[3510400 1000]})
Toposort index: 0
Inputs types: [TensorType(int32, matrix), TensorType(int32, vector)]
Inputs shapes: [(15360, 6), (2,)]
Inputs strides: [(24, 4), (4,)]
Inputs values: ['not shown', array([3510400, 1000])]
Outputs clients: [['output'], [Elemwise{Composite{(i0 + (i1 * i2))}}[(0, 2)](TensorConstant{(1, 1) of ..0.00130718}, TensorConstant{(1, 1) of 0.00261436}, mrg_uniform{TensorType(float32, matrix),inplace}.1)]]
HINT: Re-running with most Theano optimization disabled could give you a back-trace of when this node was created. This can be done with by setting the Theano flag 'optimizer=fast_compile'. If that does not work, Theano optimizations can be disabled with 'optimizer=None'.
HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint and storage map footprint of this apply node.
我的列车测试数据的尺寸20x109714 20x119778,我认为这不是一个巨大的数据集,并且它给出了此错误。 我的数据集是上述尺寸的简单numpy数组,也不是按比例缩小的图像。 我在做什么错了?