def test_set_trainable_params(): # set_trainable_params pattern mismatch with pytest.raises(KError): layer = Layer() layer.set_trainable_params('W', 1, 'b') # preserved name with pytest.raises(KError): d = Dense(1) d.set_trainable_params('trainable', 1) with pytest.raises(KError): layer = Layer() layer.set_trainable_params(1, 'W')
def test_check_input_shape(): # Class inheriting Layer does not allow mutiple inputs with pytest.raises(KError): Sequential(Dense(1)([Input(1), Input(1)])) # Input dimension mismatch with pytest.raises(KError): input1 = Input((1, 1, 1)) Dense(1)(input1) # Multiple inputs layer default accepts equal shape inputs with pytest.raises(KError): input1 = Input((1, 1, 1)) Dense(1)(input1)
def tets_fit(): # forget to compile inp1 = Input(1, batch_size=1) d1 = Dense(1) model = Model(inp1, d1) with pytest.raises(KError): model.fit([[1]], [[1]])
def test_fit_evaluate_predict_spec(): x = np.array([[1], [1]]) y = np.array([[1], [1]]) input1 = Input(1, name='input1') input2 = Input(1, name='input2') output1 = Dense(1, name='output1')(input1) output2 = Dense(1, name='output2')(input2) model0 = Model(input1, output1) model1 = Model([input1, input2], [output1, output2]) model2 = Model([input1, input2], [output1, output2]) model0.compile('sgd', 'mse', metrics=['acc']) model1.compile('sgd', loss=['mse', 'mse'], metrics=['acc']) model2.compile('sgd', loss={ 'output1': 'mse', 'output2': 'mse' }, metrics={ 'output1': 'acc', 'output2': 'acc' }) model0.predict([1, 1]) model0.evaluate([1, 1], [1, 1]) model0.fit([1, 1], [1, 1], nb_epoch=1) model1.predict([x, x]) model1.evaluate([x, x], [y, y]) model1.fit([x, x], [y, y], nb_epoch=1) model2.predict({'input1': x, 'input2': x}) model2.evaluate({'input1': x, 'input2': x}, {'output1': y, 'output2': y}) model2.fit({ 'input1': x, 'input2': x }, { 'output1': y, 'output2': y }, nb_epoch=1)
def test_compile(): inp1 = Input(1, batch_size=1) inp2 = Input(1, batch_size=2) d1 = Dense(1) d2 = Dense(1) def compile_model(inputs, outputs): model = Model(inputs, outputs) model.compile('sgd', 'mse') # input should be Input type with pytest.raises(KError): compile_model(d1, d2) with pytest.raises(KError): compile_model('whatever', d2) # input should be of Kensor type with pytest.raises(KError): compile_model(inp1, d2) # batch_size conflict with pytest.raises(KError): compile_model([inp1, inp2], d2)
def test_wrc_exceptions(): # Sequential should be initialized with a list of layer with pytest.raises(KError): Sequential(Dense(2)) # Layer weight shape mismatch with pytest.raises(KError): create_model(initial_weights={'W': np.expand_dims(W, axis=1), 'b': b}) # regularizers does not take single input with pytest.raises(KError): create_model(initial_weights=[W, b], regularizers='l1') # constraints does not take single input with pytest.raises(KError): create_model(initial_weights=[W, b], constraints='maxnorm')
def test_sgd(): ''' math: Let W = [A, B], b = [C, D], y = [E, F] MSE = 1/2*[(A+C-E)^2 + (B+D-F)^2] dA, dB, dC, dD = (A+C-E), (B+D-F), (A+C-E), (B+D-F) Assume E = 2*(A+C), F = 2*(B+D) dA, dB, dC, dD = -(A+C), -(B+D), -(A+C), -(B+D) A-=lr*dA, B-=lr*dB, C-=lr*dC, D-=lr*dD ''' lr = 0.01 W = np.array([[1, 2]]) b = np.array([3, 4]) wpb = W+b model = Sequential([Input(1), Dense(2, initial_weights=[W, b])]) optimizer = SGD(lr=lr) model.compile(optimizer, 'mse') model.fit([1], 2*wpb, nb_epoch=1) expectedW = W+lr*wpb expectedb = (b+lr*wpb).reshape((2,)) assert_allclose(B.eval(model.layers[1].W), expectedW) assert_allclose(B.eval(model.layers[1].b), expectedb)
def test_feed_exceptions(): # Forget to feed d1 with pytest.raises(KError): d1 = Dense(1) Dense(1)(d1) # Forget to feed d1 with pytest.raises(KError): d1 = Dense(1) Dense(1)(d1) # First layer of sequential should be input with pytest.raises(KError): s1 = Sequential([Dense(1)]) s1.compile('sgd', 'mse') # Recursive feeding with pytest.raises(KError): input1 = Input(1) d = Dense(1) d1 = d(input1) d(d1) # Recursive feeding with pytest.raises(KError): i1 = Input(1) i2 = Input(1) i3 = Input(1) i4 = Input(1) m = ElementWiseSum() m1 = m([i1, i2]) m2 = m([i3, i4]) m([m1, m2]) # m'th output feeds to m again # shape should be assigned as a tuple, i.e. Input((1,2)) with pytest.raises(KError): input1 = Input(1, 2) # You should not feed an Input layer with pytest.raises(KError): input1 = Input(1)(Input(1))
model.add(Convolution2D(32, 3, 3, pad='same')) model.add(Activation('relu')) model.add(Convolution2D(32, 3, 3)) model.add(Activation('relu')) model.add(Pooling1D('max', pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Convolution2D(64, 3, 3, pad='same')) model.add(Activation('relu')) model.add(Convolution2D(64, 3, 3)) model.add(Activation('relu')) model.add(Pooling1D('max', pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(512)) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(num_classes)) model.add(Activation('softmax')) # initiate RMSprop optimizer opt = keraflow.optimizers.rmsprop(lr=0.0001, decay=1e-6) # Let's train the model using RMSprop model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy']) x_train = x_train.astype('float32') x_test = x_test.astype('float32')
def test_get_tensor_shape(): # should contain _keraflow_shape with pytest.raises(KError): d = Dense(1) d.get_tensor_shape(d)
# 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(Input((1, img_rows, img_cols))) model.add(Convolution2D(nb_kernel, kernel_size[0], kernel_size[1], pad='valid')) model.add(Activation('relu')) model.add(Convolution2D(nb_kernel, kernel_size[0], kernel_size[1])) model.add(Activation('relu')) model.add(Pooling2D('max', pool_size=(nb_pool, nb_pool))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(128)) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(nb_classes)) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy']) model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch, validation_data=(X_test, Y_test)) score = model.evaluate(X_test, Y_test) print('Test score:', score[0]) print('Test accuracy:', score[1])
def create_model(**kwargs): model = Sequential([Input(1), Dense(2, **kwargs)]) model.compile('sgd', 'mse') return model
print('Loading data...') (X_train, y_train), (X_test, y_test) = imdb.load_data(nb_words=max_features) print(len(X_train), 'train sequences') print(len(X_test), 'test sequences') print('Pad sequences (samples x time)') X_train = pad_sequences(X_train, maxlen=maxlen) X_test = pad_sequences(X_test, maxlen=maxlen) print('X_train shape:', X_train.shape) print('X_test shape:', X_test.shape) print('Build model...') model = Sequential() model.add(Input(None, dtype='int32')) model.add(Embedding(max_features, 128)) model.add(LSTM(128, dropout_W=0.2, dropout_U=0.2)) # try using a GRU instead, for fun model.add(Dense(1)) model.add(Activation('sigmoid')) # try using different optimizers and different optimizer configs model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) print('Train...') model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=15, validation_data=(X_test, y_test)) score, acc = model.evaluate(X_test, y_test, batch_size=batch_size) print('Test score:', score) print('Test accuracy:', acc)
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(Input(784)) model.add(Dense(512)) 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.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))
# word group filters of size kernel_row: model.add( Convolution1D(nb_kernel=nb_kernel, kernel_row=kernel_row, pad='valid', activation='relu', stride=1)) # we use max pooling: model.add(Pooling1D('max', pool_length=maxlen - 2)) # We flatten the output of the conv layer, # so that we can add a vanilla dense layer: model.add(Flatten()) # We add a vanilla hidden layer: model.add(Dense(hidden_dims)) model.add(Dropout(0.2)) model.add(Activation('relu')) # We project onto a single unit output layer, and squash it with a sigmoid: model.add(Dense(1)) model.add(Activation('sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, validation_data=(X_test, y_test))
def dense(**kwargs): return Dense(output_dim, **kwargs)
y_train = np_utils.to_categorical(y_train, nb_classes) y_test = np_utils.to_categorical(y_test, nb_classes) row, col, pixel = X_train.shape[1:] # 4D input. x = Input(shape=(row, col, pixel)) # Encodes a row of pixels using TimeDistributed Wrapper. encoded_rows = TimeDistributed(LSTM(output_dim=row_hidden))(x) # Encodes columns of encoded rows. encoded_columns = LSTM(col_hidden)(encoded_rows) # Final predictions and model. prediction = Dense(nb_classes, activation='softmax')(encoded_columns) model = Model(inputs=x, outputs=prediction) model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy']) # Training. model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epochs, verbose=1, validation_data=(X_test, y_test)) # Evaluation. scores = model.evaluate(X_test, y_test)