我可以运行此代码,一切正常:
from keras.datasets import mnist
def plot_history(net_history):
history = net_history.history
import matplotlib.pyplot as plt
losses = history['loss']
val_losses = history['val_loss']
accuracies = history['acc']
val_accuracies = history['val_acc']
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.plot(losses)
plt.plot(val_losses)
plt.legend(['loss', 'val_loss'])
plt.figure()
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.plot(accuracies)
plt.plot(val_accuracies)
plt.legend(['acc', 'val_acc'])
# Load data
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
# Data attributes
X_train = train_images.reshape(60000, 784)
X_test = test_images.reshape(10000, 784)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
from keras.utils import np_utils
Y_train = np_utils.to_categorical(train_labels)
Y_test = np_utils.to_categorical(test_labels)
#==================================================
# Creating our model
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import SGD
from keras.losses import categorical_crossentropy
myModel = Sequential()
myModel.add(Dense(500, activation='relu', input_shape=(784,)))
myModel.add(Dropout(20))
myModel.add(Dense(100, activation='relu'))
myModel.add(Dropout(20))
myModel.add(Dense(10, activation='softmax'))
myModel.summary()
myModel.compile(optimizer=SGD(lr=0.001), loss=categorical_crossentropy,
metrics=['accuracy'])
#==================================================
# Train our model
network_history = myModel.fit(X_train, Y_train, batch_size=128, epochs=20,
validation_split=0.2)
plot_history(network_history)
但是当我无法运行code_2时:
#code_2
from keras.datasets import mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
# Data attributes
X_train = train_images.reshape(60000, 28 , 28 ,1)
X_test = test_images.reshape(10000, 28, 28, 1)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
from keras.utils import np_utils
Y_train = np_utils.to_categorical(train_labels)
Y_test = np_utils.to_categorical(test_labels)
# Creating our model
from keras.models import Model
from keras.layers import Conv2D, MaxPool2D, Input, Flatten, Dense
import keras
myInput = Input(shape=(28,28,1))
conv1 = Conv2D(16, (3,3), activation = 'relu', padding = 'same')(myInput)
pool1= MaxPool2D(pool_size = 2)(conv1)
conv2 = Conv2D(32, (3,3), activation = 'relu', padding = 'same')(pool1)
pool2= MaxPool2D(pool_size = 2)(conv2)
flat = Flatten()(pool2)
out_layer = Dense(10, activation = 'softmax')(flat)
myModel = Model(myInput, out_layer)
myModel.summary()
myModel.compile(optimizer = keras.optimizers.Adam(),
loss=keras.losses.categorical_crossentropy, metrics=['accuracy'])
#==================================================
# Train our model
network_history = myModel.fit(X_train, Y_train, batch_size=128, epochs=2,
validation_split=0.2)
ERORR
启动内核时出错 2018 15:26:08.943071:I T:\ src \ github \ tensorflow \ tensorflow \ core \ common_runtime \ gpu \ gpu_device.cc:1344]找到具有属性的设备0: 名称:GeForce GT 740M主要:3次要:5 memoryClockRate(GHz):1.0325 pciBusID:0000:01:00.0 totalMemory:2.00GiB freeMemory:1.91GiB 2018 15:26:08.943124:I T:\ src \ github \ tensorflow \ tensorflow \ core \ common_runtime \ gpu \ gpu_device.cc:1423]添加可见的gpu设备:0 2018 15:26:09.399518:I T:\ src \ github \ tensorflow \ tensorflow \ core \ common_runtime \ gpu \ gpu_device.cc:911]具有强度1边缘矩阵的设备互连StreamExecutor: 2018 15:26:09.399535:我T:\ src \ github \ tensorflow \ tensorflow \ core \ common_runtime \ gpu \ gpu_device.cc:917] 0 2018 15:26:09.399540:我T:\ src \ github \ tensorflow \ tensorflow \ core \ common_runtime \ gpu \ gpu_device.cc:930] 0:N 2018 15:26:09.399664:IT:\ src \ github \ tensorflow \ tensorflow \ core \ common_runtime \ gpu \ gpu_device.cc:1041]创建TensorFlow设备(/ job:localhost / replica:0 / task:0 / device:GPU :0和1700 MB内存) - >物理GPU(设备:0,名称:GeForce GT 740M,pci总线ID:0000:01:00.0,计算能力:3.5)
依赖关系
IPython> = 4.0:6.2.1(好)
cython> = 0.21:0.27.3(好)
jedi> = 0.9.0:0.11.1(OK)
nbconvert> = 4.0:5.3.1(确定)
numpy> = 1.7:1.14.2(OK)
pandas> = 0.13.1:0.22.0(OK)
pycodestyle> = 2.3:2.3.1(好)
pyflakes> = 0.6.0:1.6.0(OK)
pygments> = 2.0:2.2.0(OK)
pylint> = 0.25:1.8.2(OK)
qtconsole> = 4.2.0:4.3.1(确定)
rope> = 0.9.4:0.10.7(OK)
sphinx> = 0.6.6:1.6.6(OK)
sympy> = 0.7.3:1.1.1(OK)