Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keras examples doesn't work

Tags:

python

keras

I am trying to study Keras library and I tried to run this example from 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

I'm running windows 10 64bit with amd gpu, python 3.5 and keras in the latest version

like image 482
Dvir Marmor Avatar asked Jul 10 '26 15:07

Dvir Marmor


2 Answers

Unfortunately, Keras and Theano don't work well with Python 3 on Windows. 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. I would rather advice you to install it on Python 2. Here you have an exact instruction how to do it :

How do I install Keras and Theano in Anaconda Python on Windows?

like image 57
Marcin Możejko Avatar answered Jul 12 '26 08:07

Marcin Możejko


The error clearly says that it cannot find 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.

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.

like image 43
Dr. Snoopy Avatar answered Jul 12 '26 09:07

Dr. Snoopy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!