def test_flatten_layer_with_model(): model = Sequential() model.add(Embedding(2, 2)) model.add(Flatten()) model.add(Linear(4, 1)) inputs = np.array([0, 1]) out = model.feed_forward(inputs) assert out.shape == (1, )
def test_model_backprop_with_rnn(): inputs = np.array([[[1, 1], [2, 2], [3, 3], [1, 1]]]) targets = np.array([[[2, 2], [3, 3], [4, 4], [2, 2]]]) model = Sequential() model.add(RNN(2, input_shape=(4, 2))) model.skeleton() model.compile(batch_size=1, optimizer=SGD(momentum=0)) his = model.train(inputs, targets, 30, verbose=True) preds = model.predict(inputs) preds = preds.astype('int32') assert preds.shape == (1, 4, 2)
import matplotlib.pyplot as plt INPUT_SIZE = 3 HIDDEN_SIZE = 50 OUTPUT_SIZE = 1 inputs = np.array([[0,0,1], [1,1,1], [1,0,1], [0,1,1]]) outputs = np.array([0,1,1,0]) # init sequential model model = Sequential() # add layers model.add(Linear(INPUT_SIZE, OUTPUT_SIZE, activation='relu')) # we can check structure of the model model.skeleton() # we need to compile the model model.compile( loss='mse', batch_size=2, optimizer=SGD(), lr=0.001, ) # to train we just use train method
return [x >> i & 1 for i in range(10)] if __name__ == "__main__": inputs = np.array([ binary_encode(x) for x in range(101, 1024) ]) targets = np.array([ fizz_buzz_encode(x) for x in range(101, 1024) ]) print(targets) model = Sequential() model.add(Linear(10 ,50)) model.add(Tanh()) model.add(Linear(50,4)) model.skeleton() model.compile(lr=0.001) model.train(inputs, targets, 2000) #model.save_weights("test") #model.feed_forward([0,1,0,0,0,1,1,0,0,0]) # [0100001001] -> [1000] # model.compile(batch_size=32, lr=0.001) # model.train(inputs, targets, 2000)
def test_predict_tensor_expand_dim(): model = Sequential() model.add(Linear(2, 1)) preds = model.predict(np.array([1, 2])) assert preds.shape == (1, 1)
def test_predict_function_output_shape(): model = Sequential() model.add(Linear(5, 3)) preds = model.predict(np.array([[1, 2, 4, 6, 7]])) assert preds.shape == (1, 3)
def test_skeleton_function(): model = Sequential() model.add(Linear(2, 1)) model.add(RNN(1, 2)) model.skeleton()
def test_embedding_layer_with_ndim_2_input(): model = Sequential() model.add(Embedding(3, 6)) output = model.feed_forward(np.array([[0], [1], [2]])) assert output.shape == (3, 1, 6)
def test_embedding_layer_with_sequential_model(): model = Sequential() model.add(Embedding(3, 6)) word_dim = np.array([0, 1, 2]) output = model.feed_forward(word_dim) assert output.shape == (3, 6)