简体   繁体   English

keras示例不起作用

[英]keras examples doesn't work

I am trying to study Keras library and I tried to run this example from https://github.com/fchollet/keras/tree/master/examples 我正在尝试研究Keras库,并且尝试从https://github.com/fchollet/keras/tree/master/examples运行此示例

'''Trains a simple deep NN on the MNIST dataset.
Gets to 98.40% test accuracy after 20 epochs
(there is *a lot* of margin for parameter tuning).
2 seconds per epoch on a K520 GPU.
'''

from __future__ import print_function
import numpy as np
np.random.seed(1337)  # for reproducibility

from keras.datasets import mnist
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.optimizers import SGD, Adam, RMSprop
from keras.utils import np_utils


batch_size = 128
nb_classes = 10
nb_epoch = 20

# the data, shuffled and split between train and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()

X_train = X_train.reshape(60000, 784)
X_test = X_test.reshape(10000, 784)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)

model = Sequential()
model.add(Dense(512, input_shape=(784,)))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(10))
model.add(Activation('softmax'))

model.summary()

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

history = model.fit(X_train, Y_train,
                    batch_size=batch_size, nb_epoch=nb_epoch,
                    verbose=1, validation_data=(X_test, Y_test))
score = model.evaluate(X_test, Y_test, verbose=0)
print('Test score:', score[0])
print('Test accuracy:', score[1])

and than I got this error https://docs.google.com/document/d/1bo24LXbfK-NzqOBmblqM5KL91P3L3FMD1Wzq-Z5VMq0/edit?usp=sharing 然后我得到了这个错误https://docs.google.com/document/d/1bo24LXbfK-NzqOBmblqM5KL91P3L3FMD1Wzq-Z5VMq0/edit?usp=sharing

I'm running windows 10 64bit with amd gpu, python 3.5 and keras in the latest version 我正在以最新版本运行Windows 10 64bit和amd gpu,python 3.5和keras

The error clearly says that it cannot find g++.exe. 该错误明确表明它找不到g ++。exe。 Theano requires a C++ compiler to generate and compile C++ code in order to accelerate execution of the code, but seems you don't have such compiler. Theano需要C ++编译器来生成和编译C ++代码,以加快代码的执行速度,但看来您没有这种编译器。

So either install g++ (maybe from a MinGW install) and configure the paths to the g++.exe binary in theano's configuration or disable theano's C++ code generator in the configuration. 因此,要么安装g ++(也许来自MinGW安装),然后在theano的配置中配置g ++。exe二进制文件的路径,要么在配置中禁用theano的C ++代码生成器。

Tutorial: Theano install on Windows 7, 8, 10 Hello Everyone, 教程:Theano在Windows 7、8、10大家好中安装,

This post is a step by step tutorial on installing Theano for Windows 7, 8, and 10. It uses Theano, CUDA, and Anaconda. 这篇文章是有关为Windows 7、8和10安装Theano的分步教程。它使用Theano,CUDA和Anaconda。

Anaconda is a package manager for python that simplifies setting up python environments and installing dependencies. Anaconda是python的软件包管理器,可简化设置python环境和安装依赖项的过程。 If you really don't want to use Anaconda, check out my older post here. 如果您真的不想使用Anaconda,请在这里查看我的旧帖子。

Let's get to it: 让我们开始吧:

  1. Make sure your computer has a compatible CUDA graphics card: https://developer.nvidia.com/cuda-gpus 确保您的计算机具有兼容的CUDA图形卡: https : //developer.nvidia.com/cuda-gpus

  2. Download CUDA https://developer.nvidia.com/cuda-downloads (I downloaded Cuda 7.5) 下载CUDA https://developer.nvidia.com/cuda-downloads (我下载了Cuda 7.5)

  3. While that's downloading, head to https://www.visualstudio.com/en-us/downloads/download-visual-studio-vs.aspx and get Visual Studio 2013 (the community version). 在下载过程中,请转到https://www.visualstudio.com/zh-cn/downloads/download-visual-studio-vs.aspx并获取Visual Studio 2013(社区版本)。 Download and install, this will install the needed C++ compilers Couple of notes here, my install needed 7GB and took ~20 minutes to install Install CUDA ~7 minutes Note: Nsight won't install for older versions of Visual Studio if you don't have them, no worries 下载并安装,这将安装所需的C ++编译器。此处有两点说明,我的安装需要7GB的空间,安装约需20分钟,安装CUDA则需要7分钟,注意:如果您不安装Visual Studio的较早版本,则Nsight不会安装有他们,不用担心

  4. I restarted this is windows after all... 我毕竟是Windows重新启动了...

  5. Check CUDA Navigate to C:\\ProgramData\\NVIDIA Corporation\\CUDA Samples\\v7.0\\1_Utilities\\deviceQuery and open the vs2013.sln file Use CTRL+F5 to run the device check and keep the cmd window open Make sure you Pass the test, otherwise there is a problem 检查CUDA导航到C:\\ ProgramData \\ NVIDIA Corporation \\ CUDA Samples \\ v7.0 \\ 1_Utilities \\ deviceQuery并打开vs2013.sln文件,使用CTRL + F5运行设备检查并保持cmd窗口打开确保通过测试,否则有问题

  6. Download and setup Anaconda https://www.continuum.io/downloads . 下载并设置Anaconda https://www.continuum.io/downloads The Python 3.5 installer is fine Install it, it will take awhile ~5-10 minutes Python 3.5安装程序可以正常安装,大约需要5-10分钟

  7. Download Theano https://github.com/Theano/Theano , Download Zip at the bottom right Extract 下载Theano https://github.com/Theano/Theano ,在右下角下载Zip提取

  8. Open CMD prompt Setup a new conda environment that uses python 3.5 conda create -n name_of_your_environment python=3.5 打开CMD提示符设置使用python 3.5的新conda环境conda create -n name_of_your_environment python = 3.5

  9. Activate your conda environment and install dependencies activate name_of_your_environment conda install numpy scipy mingw libpython 激活conda环境并安装依赖项,以激活name_of_your_environment conda install numpy scipy mingw libpython

  10. Navigate to Theano extracted folder /Theano-master 导航到Theano提取的文件夹/ Theano-master

  11. Use python setup.py install This automatically uses 2to3 conversion 使用python setup.py install这将自动使用2to3转换

  12. We need to add some system variables 我们需要添加一些系统变量

Right click Computer -> properties -> advanced system settings -> environment variables 右键单击计算机->属性->高级系统设置->环境变量

Add a new system variable 添加一个新的系统变量

Name = THEANO_FLAGS 名称= THEANO_FLAGS

Value = floatX=float32,device=gpu,nvcc.fastmath=True 值= floatX = float32,device = gpu,nvcc.fastmath = True

Also add Visual Studio's c++ compiler to the path 还将Visual Studio的c ++编译器添加到路径

Add ;pathToYourVSInstallation\\VC\\bin\\ 添加; pathToYourVSInstallation \\ VC \\ bin \\

  1. Final check 最终检查

Open another CMD prompt (you'll need to close the old one because it doesn't have the system variables) 打开另一个CMD提示(您需要关闭旧的CMD提示,因为它没有系统变量)

activate name_of_your_environment 激活name_of_your_environment

python 蟒蛇

import theano 进口theano

You should see something like 你应该看到类似

Using gpu device 0: Quadro K1100M (CNMeM is disabled) Now you'll be able to use Theano when you activate your conda environment. 使用gpu设备0:Quadro K1100M(已禁用CNMeM)现在,您可以在激活conda环境时使用Theano。

Note: For pycharm users, PyCharm does not automatically activate the conda environment for you (bug submitted here). 注意:对于pycharm用户,PyCharm不会自动为您激活conda环境(此处提交的错误)。 What you can do is just create a .bat file with these contents: call activate env_name path_to_pycharm\\bin\\pycharm64.exe 您可以做的就是创建一个包含以下内容的.bat文件:调用activate env_name path_to_pycharm \\ bin \\ pycharm64.exe

Worked for me, windows 8.1 like a charm. 为我工作,Windows 8.1就像一个魅力。

All thanks to: http://www.islandman93.com/2016/04/tutorial-theano-install-on-windows-7-8.html 感谢: http : //www.islandman93.com/2016/04/tutorial-theano-install-on-windows-7-8.html

Unfortunately, Keras and Theano don't work well with Python 3 on Windows. 不幸的是,Keras和Theano在Windows上的Python 3不能很好地工作。 The problem you have is connected with the fact that you have to add libpython libraries to your C++ Windows Compiler and connect it with your Python installation which could be quite harsh when you have Python 3.5 installed. 您遇到的问题与以下事实有关:您必须将libpython库添加到C ++ Windows编译器,并将其与Python安装连接,而在安装Python 3.5的情况下,这可能会非常困难。 I would rather advice you to install it on Python 2. Here you have an exact instruction how to do it : 我建议您将其安装在Python 2上。在这里,您有确切的操作说明:

How do I install Keras and Theano in Anaconda Python on Windows? 如何在Windows的Anaconda Python中安装Keras和Theano?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM