如何防止在keras中创建会话失败错误?

2024-10-04 11:27:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我是tensorflow和Keras的新手,下面是我的代码

import numpy as np
np.random.seed(123)  # for reproducibility

from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.utils import np_utils
from keras.datasets import mnist

(X_train, y_train), (X_test, y_test) = mnist.load_data()

# 5. Preprocess input data
X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)
X_test = X_test.reshape(X_test.shape[0], 28, 28, 1)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255

# 6. Preprocess class labels
Y_train = np_utils.to_categorical(y_train, 10)
Y_test = np_utils.to_categorical(y_test, 10)

# 7. Define model architecture
model = Sequential()

model.add(Convolution2D(32, 3, 3, activation='relu', input_shape= 
(28,28,1)))
model.add(Convolution2D(32, 3, 3, activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))

# 8. Compile model
model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

# 9. Fit model on training data
model.fit(X_train, Y_train, 
          batch_size=32, nb_epoch=10, verbose=1)

# 10. Evaluate model on test data
score = model.evaluate(X_test, Y_test, verbose=0)

运行此代码后,我一直收到以下错误

2018-07-31 19:25:45.099850:I C:\users\nwani_bazel_nwani\mmtm6wb6\execroot\org_tensorflow\tensorflow\core\platform\cpu_特性_防护罩cc:140]您的CPU支持此TensorFlow二进制文件未编译为使用的指令:AVX AVX2 2018-07-31 19:25:45.711357:I C:\users\nwani_bazel_nwani\mmtm6wb6\execroot\org_tensorflow\tensorflow\core\common峎runtime\gpu\gpu_设备。抄送:1356]找到了 具有属性的设备0: 名称:GeForce 840M大调:5小调:0内存时钟频率(GHz):1.124 pciBusID:0000:04:00.0 总内存:2.00GiB可用内存:1.66GiB 2018-07-31 19:25:45.718218:I C:\users\nwani_bazel_nwani\mmtm6wb6\execroot\org_tensorflow\tensorflow\core\common峎runtime\gpu\gpu_设备编号:1435]正在添加可见的gpu设备:0 2018-07-31 19:25:45.722718:E C:\users\nwani_bazel_nwani\mmtm6wb6\execroot\org_tensorflow\tensorflow\core\common泳runtime\direct_会议号:154]内部:cudaGetDevice()失败。状态:CUDA驱动程序版本对于CUDA运行时版本不足 回溯(最近一次呼叫): 文件“c:\Users\Seth Siva\Documents\mnist digit识别器.py“,第44行,英寸 批处理_size=32,nb_epoch=10,verbose=1) 文件“C:\Users\Seth Siva\anaconda\lib\site packages\keras\engine\培训.py“,第1042行,in fit validation_steps=验证步骤) 文件“C:\Users\Seth Siva\anaconda\lib\site packages\keras\engine\training_数组.py“,第199行,在fit\u循环中 输出=f(输入/输出批次) 文件“C:\Users\Seth Siva\anaconda\lib\site packages\keras\backend\tensorflow_后端.py“,第2653行,in调用 如果hasattr(get_session(),''u make_callable_from_options'): 文件“C:\Users\Seth Siva\anaconda\lib\site packages\keras\backend\tensorflow_后端.py,第183行,在get_session中 _会话=tf.会议(config=配置) 文件“C:\Users\Seth Siva\anaconda\lib\site packages\tensorflow\python\client\会话.py“,第1560行,ininit super(Session,self).init(target,graph,config=config) 文件“C:\Users\Seth Siva\anaconda\lib\site packages\tensorflow\python\client\会话.py“,第633行,ininit self.\u session=tf_会话.TF\u NewSession(self.\u graph.\u c\u graph,选项) tensorflow.python.framework.错误_内部错误:无法创建会话。在


Tags: 文件frompytestimportaddmodeltensorflow