gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:27:54 25 4
gpt4 key购买 nike

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

所以在上学期的编程课上,我们使用 IDLE,但我在普通 Python 上安装 Tensorflow 时遇到问题,所以我下载了 Anaconda,并使用 conda 下载了 Keras 和 tensorflow-gpu命令,

在 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)

最佳答案

因此,在 Daniel Mö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 训练结束每个纪元的秒数​​。如果我理解正确的话,问题是link由大牛提供,与GPU内存分配有关。

关于Python 脚本在 Spyder 上运行,但如果我打开 .py 文件则不会运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50215931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com