Skip to content

Commit

Permalink
several commit in this PR (hunkim#80)
Browse files Browse the repository at this point in the history
* modify code for compatability with keras 2.x.

* change requirement Keras 1.2.2 to 2.0.2.

* fix Travis CI Build Error(First try)

* Fix Travis CI Build Error(Second try).

* Fix Travis CI Build Error(Third try).

* Fix Travis CI Build Error(Forth try).
  • Loading branch information
skyer9 authored and hunkim committed Apr 2, 2017
1 parent be1b26c commit 8a8a5a3
Show file tree
Hide file tree
Showing 22 changed files with 94 additions and 69 deletions.
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# code below is taken from https://github.com/fchollet/keras/blob/master/.travis.yml
sudo: required
dist: trusty
env:
- LD_PRELOAD="/usr/lib/libtcmalloc.so.4"
language: python
python: # Only two versions for now
- "2.7"
- "3.5"
# command to install dependencies
# command to install dependencies
before_install:
- sudo apt-get install google-perftools
install: "pip install -r requirements.txt"

script:
Expand All @@ -17,7 +20,7 @@ script:
- sed -i -- 's/nb_epoch = 12/nb_epoch = 1/g' *lab-*.py # change range to 1 for quick testing
- sed -i -- 's/plot(/# plot (/g' *lab-*.py # change range to 1 for quick testing
- sed -i -- 's/from keras.utils.visualize_util import plot/#/g' *lab-*.py
- python lab-07-2-learning_rate_and_evaluation.py # run this first to download the MNIST file
- python lab-07-4-mnist_introduction.py # run this first to download the MNIST file
# run all python files in parallel, http://stackoverflow.com/questions/5015316
# The log length has exceeded the limit of 4 MB (this usually means that the test suite is raising the same exception over and over).
#- ls lab-*.py|xargs -n 1 -P 1 python > /dev/null
Expand Down
2 changes: 1 addition & 1 deletion Keras/klab-02-1-linear_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# prints summary of the model to the terminal
model.summary()

model.fit(x_train, y_train, nb_epoch=1000)
model.fit(x_train, y_train, epochs=1000)

y_predict = model.predict(np.array([4]))
print(y_predict)
6 changes: 3 additions & 3 deletions Keras/klab-04-1-multi_input_linear_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
y_data = [[152.], [185.], [180.], [196.], [142.]]

model = Sequential()
model.add(Dense(output_dim=1, input_dim=3))
model.add(Dense(input_dim=3, units=1))
model.add(Activation('linear'))

model.compile(loss='mse', optimizer='rmsprop', lr=1e-10)
model.fit(x_data, y_data, nb_epoch=1000)
model.compile(loss='mse', optimizer='rmsprop', lr=1e-10)
model.fit(x_data, y_data, epochs=1000)

y_predict = model.predict(np.array([[95., 100., 80]]))
print(y_predict)
2 changes: 1 addition & 1 deletion Keras/klab-04-2-multi_input_linear_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
y_data = [[152.], [185.], [180.], [196.], [142.]]

model = Sequential()
model.add(Dense(input_dim=3, output_dim=1))
model.add(Dense(input_dim=3, units=1))

model.compile(loss='mse', optimizer='rmsprop')
model.fit(x_data, y_data)
Expand Down
2 changes: 1 addition & 1 deletion Keras/klab-04-3-file_input_linear_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
print("y_data", y_data)

model = Sequential()
model.add(Dense(input_dim=3, output_dim=1))
model.add(Dense(input_dim=3, units=1))

model.compile(loss='mse', optimizer='rmsprop')
model.fit(x_data, y_data, nb_epoch=2000)
Expand Down
11 changes: 6 additions & 5 deletions Keras/klab-04-4-stock_linear_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import numpy as np
from sklearn.preprocessing import MinMaxScaler

import matplotlib.pyplot as plt

xy = np.loadtxt('data-02-stock_daily.csv', delimiter=',')

# very important. It does not work without it.
Expand All @@ -24,10 +26,10 @@
print(x_data[0], "->", y_data[0])

model = Sequential()
model.add(Dense(input_dim=4, output_dim=1))
model.add(Dense(input_dim=4, units=1))

model.compile(loss='mse', optimizer='sgd', metrics=['mse'])
model.fit(x_data, y_data, nb_epoch=100)
model.fit(x_data, y_data, epochs=100)

test = x_data[10].reshape(-1, 4)
print("y=", y_data[10], "prediction=", model.predict(test))
Expand All @@ -38,19 +40,18 @@
# ---------------------------
# Test
# split to train and testing
import matplotlib.pyplot as plt

train_size = int(len(x_data) * 0.7)
test_size = len(x_data) - train_size
x_train, x_test = x_data[0:train_size], x_data[train_size:len(x_data)]
y_train, y_test = y_data[0:train_size], y_data[train_size:len(y_data)]

model = Sequential()
model.add(Dense(input_dim=4, output_dim=1))
model.add(Dense(input_dim=4, units=1))
model.compile(loss='mse', optimizer='sgd', metrics=['mse'])

# Train a model
model.fit(x_train, y_train, nb_epoch=200)
model.fit(x_train, y_train, epochs=200)

# evaluate
results = model.evaluate(x_test, y_test, verbose=1)
Expand Down
2 changes: 1 addition & 1 deletion Keras/klab-05-1-logistic_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
model.compile(loss='binary_crossentropy', optimizer='sgd', lr=0.1)

model.summary()
model.fit(x_data, y_data, nb_epoch=2000)
model.fit(x_data, y_data, epochs=2000)

print("2,1", model.predict_classes(np.array([[2, 1]])))
print("6,5", model.predict_classes(np.array([[6, 5]])))
2 changes: 1 addition & 1 deletion Keras/klab-06-1-softmax.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
optimizer='sgd',
metrics=['accuracy'])

history = model.fit(x_data, y_data, nb_epoch=1000)
history = model.fit(x_data, y_data, epochs=1000)

print(model.predict_classes(np.array([[1, 2, 1, 1]])))
print(model.predict_classes(np.array([[1, 2, 5, 6]])))
2 changes: 1 addition & 1 deletion Keras/klab-06-2-softmax_zoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
optimizer='sgd',
metrics=['accuracy'])

history = model.fit(x_data, y_one_hot, nb_epoch=1000)
history = model.fit(x_data, y_one_hot, epochs=1000)

# Let's see if we can predict
pred = model.predict_classes(x_data)
Expand Down
2 changes: 1 addition & 1 deletion Keras/klab-09-1-xor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
model.compile(loss='binary_crossentropy', optimizer='sgd',
lr=0.1, metrics=['accuracy'])
model.summary()
model.fit(x_data, y_data, nb_epoch=50000)
model.fit(x_data, y_data, epochs=50000)

score = model.evaluate(x_data, y_data, verbose=0)
print('Test score:', score[0])
Expand Down
1 change: 0 additions & 1 deletion Keras/klab-09-2-xor-nn.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD

x_data = [[0., 0.], [0., 1.], [1., 0.], [1., 1.]]
y_data = [[0.], [1.], [1.], [0.]]
Expand Down
3 changes: 2 additions & 1 deletion Keras/klab-11-1-cnn_mnist.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

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
Expand All @@ -16,6 +15,8 @@
from keras.utils import np_utils
from keras import backend as K

np.random.seed(1337) # for reproducibility

batch_size = 128
nb_classes = 10
nb_epoch = 12
Expand Down
19 changes: 10 additions & 9 deletions Keras/klab-12-1-rnn_hello_char.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

# brew install graphviz
# pip3 install graphviz
# pip3 install pydot
from keras.utils.visualize_util import plot
# pip3 install pydot-ng
from keras.utils.vis_utils import plot_model

# sample text
sample = "hihello"
Expand All @@ -21,37 +21,38 @@

data_dim = len(char_set)
timesteps = len(y_str)
nb_classes = len(char_set)
num_classes = len(char_set)

print(x_str, y_str)

x = [char_dic[c] for c in x_str] # char to index
y = [char_dic[c] for c in y_str] # char to index

# One-hot encoding
x = np_utils.to_categorical(x, nb_classes=nb_classes)
x = np_utils.to_categorical(x, num_classes=num_classes)
# reshape X to be [samples, time steps, features]
x = np.reshape(x, (-1, len(x), data_dim))
print(x.shape)

# One-hot encoding
y = np_utils.to_categorical(y, nb_classes=nb_classes)
y = np_utils.to_categorical(y, num_classes=num_classes)
# time steps
y = np.reshape(y, (-1, len(y), data_dim))
print(y.shape)

model = Sequential()
model.add(LSTM(nb_classes, input_shape=(
model.add(LSTM(num_classes, input_shape=(
timesteps, data_dim), return_sequences=True))
model.add(TimeDistributed(Dense(nb_classes)))
model.add(TimeDistributed(Dense(num_classes)))
model.add(Activation('softmax'))
model.summary()
# Store model graph in png
# plot(model, to_file=os.path.basename(__file__) + '.png', show_shapes=True)
# (Error occurs on in python interactive shell)
plot_model(model, to_file=os.path.basename(__file__) + '.png', show_shapes=True)

model.compile(loss='categorical_crossentropy',
optimizer='rmsprop', metrics=['accuracy'])
model.fit(x, y, nb_epoch=1)
model.fit(x, y, epochs=1)

predictions = model.predict(x, verbose=0)
for i, prediction in enumerate(predictions):
Expand Down
17 changes: 9 additions & 8 deletions Keras/klab-12-1-softmax_hello_char.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

# brew install graphviz
# pip3 install graphviz
# pip3 install pydot
from keras.utils.visualize_util import plot
# pip3 install pydot-ng
from keras.utils.vis_utils import plot_model

# sample text
sample = "hihello"
Expand All @@ -21,36 +21,37 @@

data_dim = len(char_set)
timesteps = len(y_str)
nb_classes = len(char_set)
num_classes = len(char_set)

print(x_str, y_str)

x = [char_dic[c] for c in x_str] # char to index
y = [char_dic[c] for c in y_str] # char to index

# One-hot encoding
x = np_utils.to_categorical(x, nb_classes=nb_classes)
x = np_utils.to_categorical(x, num_classes=num_classes)
# reshape X to be [samples, time steps, features]
x = np.reshape(x, (-1, len(x), data_dim))
print(x.shape)

# One-hot encoding
y = np_utils.to_categorical(y, nb_classes=nb_classes)
y = np_utils.to_categorical(y, num_classes=num_classes)
# time steps
y = np.reshape(y, (-1, len(y), data_dim))
print(y.shape)

model = Sequential()
model.add(Dense(nb_classes, input_shape=(
model.add(Dense(num_classes, input_shape=(
timesteps, data_dim)))
model.add(Activation('softmax'))
model.summary()
# Store model graph in png
# plot(model, to_file=os.path.basename(__file__) + '.png', show_shapes=True)
# (Error occurs on in python interactive shell)
plot_model(model, to_file=os.path.basename(__file__) + '.png', show_shapes=True)

model.compile(loss='categorical_crossentropy',
optimizer='rmsprop', metrics=['accuracy'])
model.fit(x, y, nb_epoch=100)
model.fit(x, y, epochs=100)

predictions = model.predict(x, verbose=0)
for i, prediction in enumerate(predictions):
Expand Down
21 changes: 11 additions & 10 deletions Keras/klab-12-2-rnn_long_char.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

# brew install graphviz
# pip3 install graphviz
# pip3 install pydot
from keras.utils.visualize_util import plot
# pip3 install pydot-ng
from keras.utils.vis_utils import plot_model

# sample sentence
sentence = ("if you want to build a ship, don't drum up people together to "
Expand All @@ -20,7 +20,7 @@

data_dim = len(char_set)
seq_length = timesteps = 10
nb_classes = len(char_set)
num_classes = len(char_set)

dataX = []
dataY = []
Expand All @@ -36,32 +36,33 @@
dataY.append(y)

# One-hot encoding
dataX = np_utils.to_categorical(dataX, nb_classes=nb_classes)
dataX = np_utils.to_categorical(dataX, num_classes=num_classes)
# reshape X to be [samples, time steps, features]
dataX = np.reshape(dataX, (-1, seq_length, data_dim))
print(dataX.shape)

# One-hot encoding
dataY = np_utils.to_categorical(dataY, nb_classes=nb_classes)
dataY = np_utils.to_categorical(dataY, num_classes=num_classes)
# time steps
dataY = np.reshape(dataY, (-1, seq_length, data_dim))
print(dataY.shape)

model = Sequential()
model.add(LSTM(nb_classes, input_shape=(
model.add(LSTM(num_classes, input_shape=(
timesteps, data_dim), return_sequences=True))
model.add(LSTM(nb_classes, return_sequences=True))
model.add(TimeDistributed(Dense(nb_classes)))
model.add(LSTM(num_classes, return_sequences=True))
model.add(TimeDistributed(Dense(num_classes)))

model.add(Activation('softmax'))
model.summary()

# Store model graph in png
# plot(model, to_file=os.path.basename(__file__) + '.png', show_shapes=True)
# (Error occurs on in python interactive shell)
plot_model(model, to_file=os.path.basename(__file__) + '.png', show_shapes=True)

model.compile(loss='categorical_crossentropy',
optimizer='rmsprop', metrics=['accuracy'])
model.fit(dataX, dataY, nb_epoch=1000)
model.fit(dataX, dataY, epochs=1000)

predictions = model.predict(dataX, verbose=0)
for i, prediction in enumerate(predictions):
Expand Down
9 changes: 5 additions & 4 deletions Keras/klab-12-3-rnn_stock_prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

# brew install graphviz
# pip3 install graphviz
# pip3 install pydot
from keras.utils.visualize_util import plot
# pip3 install pydot-ng
from keras.utils.vis_utils import plot_model

import matplotlib.pyplot as plt

Expand Down Expand Up @@ -52,10 +52,11 @@
model.summary()

# Store model graph in png
# plot(model, to_file=os.path.basename(__file__) + '.png', show_shapes=True)
# (Error occurs on in python interactive shell)
plot_model(model, to_file=os.path.basename(__file__) + '.png', show_shapes=True)

print(trainX.shape, trainY.shape)
model.fit(trainX, trainY, nb_epoch=200)
model.fit(trainX, trainY, epochs=200)

# make predictions
testPredict = model.predict(testX)
Expand Down
9 changes: 5 additions & 4 deletions Keras/klab-12-4-rnn_deep_prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

# brew install graphviz
# pip3 install graphviz
# pip3 install pydot
from keras.utils.visualize_util import plot
# pip3 install pydot-ng
from keras.utils.vis_utils import plot_model

import matplotlib.pyplot as plt

Expand Down Expand Up @@ -54,12 +54,13 @@
model.summary()

# Store model graph in png
# plot(model, to_file=os.path.basename(__file__) + '.png', show_shapes=True)
# (Error occurs on in python interactive shell)
plot_model(model, to_file=os.path.basename(__file__) + '.png', show_shapes=True)

model.compile(loss='mean_squared_error', optimizer='adam')

print(trainX.shape, trainY.shape)
model.fit(trainX, trainY, nb_epoch=200)
model.fit(trainX, trainY, epochs=200)

# make predictions
testPredict = model.predict(testX)
Expand Down
Loading

0 comments on commit 8a8a5a3

Please sign in to comment.