#model.add(Dense(nb_actions)) #model.compile(RMSprop(), 'MSE') agent = Agent(model=model, memory_size=-1, nb_frames=nb_frames) #model.save('/tmp/snake1.hdf5') #agent.train(snake, batch_size=64, nb_epoch=10000, gamma=0.8) #model.save('/tmp/snake2.hdf5') #agent.play(snake) snake.reset() agent.clear_frames() S = agent.get_game_data(snake) game_over = False frames = list() frames.append(S[0]) while not game_over: q = model.predict(S)[0] possible_actions = snake.get_possible_actions() q = [q[i] for i in possible_actions] action = possible_actions[np.argmax(q)] snake.play(action) S = agent.get_game_data(snake) frames.append(S[0]) game_over = snake.is_over() print(np.asarray(frames).shape) kerasimo.ToSVG('snake', model, np.array(frames), showarrows=False, columns=[1, 3, 3, 10, 10, 1])
X = list() for i in range(0, ntest): X.append(np.empty((5, 7, 1))) for jj in range(0, 7): for ii in range(0, 5): if (digits[i * 7 + jj][ii] == '*'): X[i][ii][jj][0] = 1. else: X[i][ii][jj][0] = 0. X = np.array(X) Y = np.identity(ntest) model = Sequential() model.add(ZeroPadding2D(padding=1, input_shape=(5, 7, 1))) model.add(Conv2D(2, (2, 2), activation='relu')) model.add(MaxPooling2D(pool_size=(3, 3))) model.add(Dropout(0.10)) model.add(Flatten()) model.add(Dense(ntest, activation='sigmoid')) sgd = SGD(lr=0.1) model.compile(loss='binary_crossentropy', optimizer=sgd) model.fit(X, Y, batch_size=1, epochs=2000) print(model.predict_proba(X)) kerasimo.ToSVG('digits_cnn', model, X)
from keras.models import Sequential from keras.layers.core import Dense, Dropout, Activation from keras.optimizers import SGD import numpy as np from lib import kerasimo X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) y = np.array([[0], [1], [1], [0]]) model = Sequential() model.add(Dense(2, activation='tanh', input_dim=2)) model.add(Dense(1, activation='sigmoid')) sgd = SGD(lr=0.1) model.compile(loss='binary_crossentropy', optimizer=sgd) model.fit(X, y, batch_size=1, epochs=1000) print(model.predict_proba(X)) kerasimo.ToSVG('xor', model, X)
for ii in range(0, 5): if (digits[i*7+jj][ii] == '*'): X[i][jj*5+ii] = 1 X = np.array(X); print(X); Y = np.array([ [1,0,0,0,0,0,0,0,0,0], [0,1,0,0,0,0,0,0,0,0], [0,0,1,0,0,0,0,0,0,0], [0,0,0,1,0,0,0,0,0,0], [0,0,0,0,1,0,0,0,0,0], [0,0,0,0,0,1,0,0,0,0], [0,0,0,0,0,0,1,0,0,0], [0,0,0,0,0,0,0,1,0,0], [0,0,0,0,0,0,0,0,1,0], [0,0,0,0,0,0,0,0,0,1] ]) model = Sequential() model.add(Dense(10, activation='sigmoid', input_dim=35)) sgd = SGD(lr=0.1) model.compile(loss='binary_crossentropy', optimizer=sgd) model.fit(X, Y, batch_size=1, epochs=2000) print(model.predict_proba(X)) kerasimo.ToSVG('digits', model, X, columns=[5, 1])
from keras.models import Sequential from keras.layers.core import Dense, Dropout, Activation from keras.optimizers import SGD import numpy as np from lib import kerasimo X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) y = np.array([[0], [0], [0], [1]]) model = Sequential() model.add(Dense(1, activation='sigmoid', input_dim=2)) sgd = SGD(lr=0.1) model.compile(loss='binary_crossentropy', optimizer=sgd) model.fit(X, y, batch_size=1, epochs=1000) print(model.predict_proba(X)) kerasimo.ToSVG('and', model, X)
model.add( Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape)) model.add(Conv2D(64, (3, 3), activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(128, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(num_classes, activation='softmax')) model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adadelta(), metrics=['accuracy']) model.fit(X_train, Y_train, batch_size=batch_size, epochs=epochs, 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]) kerasimo.ToSVG('mnist_cnn', model, X_test[20:40], columns=[1, 4, 4, 4, 1, 1, 1, 1, 1, 1, 1], showarrows=False)
from keras.models import Sequential from keras.layers.core import Dense, Dropout, Activation from keras.optimizers import SGD import numpy as np from lib import kerasimo X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) y = np.array([[0], [1], [1], [0]]) model = Sequential() model.add(Dense(1, activation='sigmoid', input_dim=2)) sgd = SGD(lr=0.1) model.compile(loss='binary_crossentropy', optimizer=sgd) model.fit(X, y, batch_size=1, epochs=1000) print(model.predict_proba(X)) kerasimo.ToSVG('xor_wrong', model, X)
import keras print(sys.argv) X = np.array([ [1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 1], ]) model = Sequential() model.add(Dense(int(sys.argv[1]), activation='tanh', input_dim=8)) if (int(sys.argv[2]) != 0): model.add(Dense(int(sys.argv[2]), activation='tanh')) model.add(Dense(8, activation='sigmoid')) sgd = SGD(lr=0.1) model.compile(loss='binary_crossentropy', optimizer=sgd) model.fit(X, X, batch_size=1, epochs=10000) print(model.predict_proba(X)) kerasimo.ToSVG('encode%d%d' % (int(sys.argv[1]), int(sys.argv[2])), model, X)