在Spyder上运行的Python脚本,但是如果我打开.py文件则不会

时间:2018-05-07 13:51:37

标签: python tensorflow keras anaconda spyder

我试图在Python中使用Keras创建一个网络,用于识别MNIST库中用于类项目的手写数字。

所以在编程类的最后一个semmester我们使用IDLE但是我在库存python上安装Tensorflow时遇到了问题所以我下载了Anaconda并使用tensorflow-gpu命令下载了Keras和conda

在Spyder中,一切正常,但是当我打开.py文件时,我收到了一堆错误,程序崩溃了。

我确认该文件是使用来自Anaconda安装路径的python.exe打开的。我正在附加我的代码和错误。

from keras.datasets import mnist
import matplotlib.pyplot as plt
import numpy
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.utils import np_utils
from keras import backend as K
K.set_image_dim_ordering('th')

def small_CNN():
    model = Sequential()

    model.add(Conv2D(32, (5, 5), input_shape = (1, 28, 28), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.2))
    model.add(Flatten())
    model.add(Dense(128, activation='relu'))
    model.add(Dense(classes, activation='softmax'))

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

    return model


def big_CNN():
    model=Sequential()
    model.add(Conv2D(30, (5,5), input_shape = (1, 28, 28), activation = 'relu'))
    model.add(MaxPooling2D(pool_size = (2, 2)))
    model.add(Conv2D(15, (3, 3), activation = 'relu'))
    model.add(MaxPooling2D(pool_size = (2, 2)))
    model.add(Dropout(0.2))
    model.add(Flatten())
    model.add(Dense(128, activation = 'relu'))
    model.add(Dense(50, activation = 'relu'))
    model.add(Dense(classes, activation = 'softmax'))

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

    return model

numpy.random.seed(7)

(d_train, c_train), (d_test, c_test) = mnist.load_data()

d_train = d_train.reshape(d_train.shape[0], 1, 28, 28).astype('float32') / 255
d_test = d_test.reshape(d_test.shape[0], 1, 28, 28).astype('float32') / 255

c_train = np_utils.to_categorical(c_train)
c_test = np_utils.to_categorical(c_test)
classes = c_test.shape[1]

model = big_CNN()
model.fit(d_train, c_train, validation_data = (d_test, c_test), epochs = 5, batch_size = 200, verbose = 1)
scores2 = model.evaluate(d_test, c_test, verbose=0)
print(str(scores2[1]*100)+"%")

错误:

Using TensorFlow backend.
2018-05-07 17:14:42.643244: W c:\l\work\tensorflow-1.1.0\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE instructions, but these are available on your machine and could speed up CPU computations.
2018-05-07 17:14:42.646784: W c:\l\work\tensorflow-1.1.0\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE2 instructions, but these are available on your machine and could speed up CPU computations.
2018-05-07 17:14:42.650180: W c:\l\work\tensorflow-1.1.0\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE3 instructions, but these are available on your machine and could speed up CPU computations.
2018-05-07 17:14:42.653900: W c:\l\work\tensorflow-1.1.0\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2018-05-07 17:14:42.657146: W c:\l\work\tensorflow-1.1.0\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2018-05-07 17:14:42.660899: W c:\l\work\tensorflow-1.1.0\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2018-05-07 17:14:42.664397: W c:\l\work\tensorflow-1.1.0\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2018-05-07 17:14:42.667624: W c:\l\work\tensorflow-1.1.0\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
2018-05-07 17:14:42.961063: I c:\l\work\tensorflow-1.1.0\tensorflow\core\common_runtime\gpu\gpu_device.cc:887] Found device 0 with properties:
name: GeForce GTX 960
major: 5 minor: 2 memoryClockRate (GHz) 1.291
pciBusID 0000:01:00.0
Total memory: 2.00GiB
Free memory: 1.12GiB
2018-05-07 17:14:42.965514: I c:\l\work\tensorflow-1.1.0\tensorflow\core\common_runtime\gpu\gpu_device.cc:908] DMA: 0
2018-05-07 17:14:42.967183: I c:\l\work\tensorflow-1.1.0\tensorflow\core\common_runtime\gpu\gpu_device.cc:918] 0:   Y
2018-05-07 17:14:42.969153: I c:\l\work\tensorflow-1.1.0\tensorflow\core\common_runtime\gpu\gpu_device.cc:977] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 960, pci bus id: 0000:01:00.0)
2018-05-07 17:14:43.781059: I c:\l\work\tensorflow-1.1.0\tensorflow\core\common_runtime\gpu\gpu_device.cc:977] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 960, pci bus id: 0000:01:00.0)
Train on 60000 samples, validate on 10000 samples
Epoch 1/5
2018-05-07 17:14:45.912647: E c:\l\work\tensorflow-1.1.0\tensorflow\stream_executor\cuda\cuda_dnn.cc:359] could not create cudnn handle: CUDNN_STATUS_NOT_INITIALIZED
2018-05-07 17:14:45.915318: E c:\l\work\tensorflow-1.1.0\tensorflow\stream_executor\cuda\cuda_dnn.cc:366] error retrieving driver version: Unimplemented: kernel reported driver version not implemented on Windows
2018-05-07 17:14:45.925473: E c:\l\work\tensorflow-1.1.0\tensorflow\stream_executor\cuda\cuda_dnn.cc:326] could not destroy cudnn handle: CUDNN_STATUS_BAD_PARAM
2018-05-07 17:14:45.928009: F c:\l\work\tensorflow-1.1.0\tensorflow\core\kernels\conv_ops.cc:659] Check failed: stream->parent()->GetConvolveAlgorithms(&algorithms)

1 个答案:

答案 0 :(得分:1)

所以在DanielMöller的帮助下,我能够通过在我的代码开头添加这些行来解决问题(在我导入所有内容之后):

import tensorflow as tf
from keras.backend.tensorflow_backend import set_session
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
set_session(tf.Session(config=config))

有趣的是,spyder训练每个时期花了大约3-5秒,现在需要20-25当我打开.py文件时,我仍然得到所有警告但是它正确运行直到3-5结束训练每个时期的秒数。如果我从Daniel提供的这个link中正确理解的话,这个问题与GPU内存分配有关。