Issue
I'm running code from here.
python mycode.py
It runs without issue, producing expected output.
However, when I add a line to the top:
#!/usr/bin/python
and run
chmod +x mycode.py
and then this:
./mycode.py
I get the following error:
Traceback (most recent call last):
File "./mycode.py", line 3, in <module>
from keras.models import Sequential
ImportError: No module named keras.models
My end goal is to modify the code to accept things like --epochs from the command line. How can I do this with Keras?
EDIT:
Here's the code that works (run using python mycode.py). You can obtain the dataset here (a csv, not huge). The problem comes up when I add the shebang to the top and chmod and run the alternate way.
from keras.models import Sequential
from keras.layers import Dense
import numpy
# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# load pima indians dataset
dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
# create model
model = Sequential()
model.add(Dense(12, input_dim=8, init='uniform', activation='relu'))
model.add(Dense(8, init='uniform', activation='relu'))
model.add(Dense(1, init='uniform', activation='sigmoid'))
# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(X, Y, nb_epoch=2, batch_size=10)
# evaluate the model
scores = model.evaluate(X, Y)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
Solution
Most likely, the reason is here: #!/usr/bin/python
. When you running python
from console it can use another python interpreter (e.g. you have it installed at /usr/local/bin/python
). So you need to use the same in your script, the easiest way is to declare it like #!/usr/bin/env python
Answered By - outcatcher Answer Checked By - Marie Seifert (WPSolving Admin)