def test_lambda(): # TODO serialization has problem import keraflow.backend as B layer_test(core.Lambda(lambda x: x**2), [origin], [origin**2], test_serialization=False) layer_test(core.Lambda(lambda x: B.concatenate([x**2, x], axis=1), lambda s: (s[0],2*s[1])+s[2:]), [origin], [np.concatenate([origin**2, origin], axis=0)], test_serialization=False) shape = list(origin.shape) shape[0] *=2 layer_test(core.Lambda(lambda x: B.concatenate([x**2, x], axis=1), shape), [origin], [np.concatenate([origin**2, origin], axis=0)], test_serialization=False) # output_shape_fn should be a list, a tuple, or a function with pytest.raises(KError): layer_test(core.Lambda(lambda x: x**2, 1), [origin], [np.concatenate([origin**2, origin], axis=0)], test_serialization=False) # output_shape_fn should return a list or a tuple with pytest.raises(KError): layer_test(core.Lambda(lambda x: x**2, lambda x: 1), [origin], [np.concatenate([origin**2, origin], axis=0)], test_serialization=False)
def test_conv2D(): settings = [(6,3,(1,1)), (6,3,(1,2)), (6,3,(1,3))] def get_data(row, kernel_row, strides, padding): x = 1+np.arange(row)[:,None]*np.array([1]*row) x = x[None, :,:] W = np.ones((kernel_row, kernel_row))[None,None,:,:] b = np.array([0]) exp_output = tf_conv2d(x, W, strides, padding) return x, W, b, exp_output # testing: padding=valid, bias=True # pad_num is always zero, test jsut 1 setting for row, kernel_row, strides in settings[:1]: x, W, b, exp_output = get_data(row, kernel_row, strides, 'valid') layer_test(convolution.Convolution2D(1, kernel_row, kernel_row, strides=strides, bias=True, pad='valid', initial_weights=[W, b]), [x], [exp_output]) # testing: padding=same, bias=False # Note that we do not test serializetion here to save io. for row, kernel_row, strides in settings: x, W, b, exp_output = get_data(row, kernel_row, strides, 'same') layer_test(convolution.Convolution2D(1, kernel_row, kernel_row, strides=strides, bias=False, pad='same', initial_weights=[W]), [x], [exp_output], test_serialization=False)
def test_padding1D(): x = np.random.rand(3,3) pad = (1,2) np_pad = ((1,2),(0,0)) layer_test(convolution.ZeroPadding1D(pad), [x], [np.pad(x, np_pad, 'constant')])
def test_permute_dims(): dims = [1, 0] layer_test(core.PermuteDims(dims), [origin], [np.transpose(origin, dims)]) layer_test(core.PermuteDims([0, 2, 1], include_batch_dim=True), [origin], [np.transpose(origin, dims)])
def test_repeat(): axis = 1 layer_test(core.Repeat(2, axis), [origin], [np.repeat(origin, 2, axis)]) layer_test(core.Repeat(2, axis, include_batch_dim=True), [origin], [np.repeat(origin, 2, axis-1)])
def test_expand_dims(): axis=2 layer_test(core.ExpandDims(axis=axis), [origin], [np.expand_dims(origin, axis)]) layer_test(core.ExpandDims(axis=axis, include_batch_dim=True), [origin], [np.expand_dims(origin, axis-1)])
def test_dense(): W = np.array([[1, 2]]) b = np.array([3, 4]) layer_test(core.Dense(2, initial_weights=[W, b]), [[1]], W+b) layer_test(core.Dense(2, initial_weights=[W], bias=False), [[1]], W)
def test_unsampling3D(): x = np.random.rand(3,3,3,3) size=(2,2,2) exp_output = x for axis in [1,2,3]: exp_output = np.repeat(exp_output, 2, axis=axis) layer_test(convolution.UnSampling3D(size), [x], [exp_output])
def test_padding2D(): x = np.random.rand(3,3,3) pad = ((1,2),1) np_pad = ((0,0), (1,2),(1,1)) layer_test(convolution.ZeroPadding2D(pad), [x], [np.pad(x, np_pad, 'constant')]) # size could take no more than two values with pytest.raises(KError): layer_test(convolution.ZeroPadding2D(((1,2,3),1)), [x], [np.pad(x, np_pad, 'constant')])
def run_test(RNNCls, step, num_states, **cls_args): layer_test(RNNCls(output_dim, **cls_args), [origin], rnn([origin], step, output_dim, num_states)) # test dropout, just test if it can pass... layer_test(RNNCls(output_dim, dropout_W=0.2, dropout_U=0.2, **cls_args), [origin], test_serialization=False) # test return_sequences, go_backwards, unroll layer_test( RNNCls(output_dim, return_sequences=True, go_backwards=True, unroll=True, **cls_args), [origin], rnn([origin], step, output_dim, num_states, return_sequences=True, go_backwards=True)) # test stateful rnn_layer = RNNCls(output_dim, stateful=True, **cls_args) exp_output = rnn([origin], step, output_dim, num_states) layer_test(rnn_layer, [origin], exp_output, input_args=dict(batch_size=1)) assert_allclose(B.eval(rnn_layer.states[0]), exp_output) rnn_layer.reset_states() assert not np.any(B.eval(rnn_layer.states[0]))
def test_concatenate(): reduced = origin[:, :-1] layer_test(core.Concatenate(axis=1), [[origin], [reduced]], [np.concatenate([origin, reduced], axis=1)], multi_input=True) layer_test(core.Concatenate(axis=2, include_batch_dim=True), [[origin], [reduced]], [np.concatenate([origin, reduced], axis=1)], multi_input=True) with pytest.raises(KError): # cant not caoncatenate tensor of different dimension layer_test(core.Concatenate(axis=2, include_batch_dim=True), [[origin], [np.expand_dims(reduced,1)]], [np.concatenate([origin, reduced], axis=1)], multi_input=True) with pytest.raises(KError): # cant not caoncatenate tensor of of mismatched shape layer_test(core.Concatenate(axis=2, include_batch_dim=True), [[origin], [origin[:-1, :-1]]], [np.concatenate([origin, reduced], axis=1)], multi_input=True)
def test_pooling1D(): settings = [(8, 4, 1), (8, 4, 2), (8, 4, 3), (8, 4, 4)] # testing: padding=valid # pad_num is always zero, test jsut 1 setting for row, pool_length, stride in settings[:1]: x = 1 + np.arange(row)[:, None] * np.array([1] * pool_length) layer_test( convolution.Pooling1D('max', pool_length, pad='valid', stride=stride), [x]) layer_test(convolution.Pooling1D('avg', pool_length, pad='valid', stride=stride), [x], debug=True) # testing: padding=same # Note that we do not test serializetion here to save io. for row, pool_length, stride in settings: x = 1 + np.arange(row)[:, None] * np.array([1] * pool_length) layer_test(convolution.Pooling1D('max', pool_length, pad='same', stride=stride), [x], test_serialization=False) layer_test(convolution.Pooling1D('avg', pool_length, pad='same', stride=stride), [x], test_serialization=False)
def test_pooling3D(): settings = [(8,4,(4,4,1)), (8,4,(4,3,2)), (8,4,(4,2,3)), (8,4,(4,1,4)), (8,4,(4,4,4))] # testing: padding=valid # pad_num is always zero, test jsut 1 setting for row, pool_row, strides in settings[:1]: pool_size = (pool_row, pool_row, pool_row) x = 1+np.arange(row)[:,None]*np.array([1]*row) x = np.stack([int(c)*x for c in np.linspace(-100,100,row)]) x = x[None, :,:, :] layer_test(convolution.Pooling3D('max', pool_size, pad='valid', strides=strides), [x]) layer_test(convolution.Pooling3D('avg', pool_size, pad='valid', strides=strides), [x]) # testing: padding=same # Note that we do not test serializetion here to save io. for row, pool_row, strides in settings: pool_size = (pool_row, pool_row, pool_row) x = 1+np.arange(row)[:,None]*np.array([1]*row) x = np.stack([int(c)*x for c in np.linspace(-100,100,row)]) x = x[None, :,:, :] layer_test(convolution.Pooling3D('max', pool_size, pad='same', strides=strides), [x], test_serialization=False) layer_test(convolution.Pooling3D('avg', pool_size, pad='same', strides=strides), [x], test_serialization=False)
def test_embedding(): vocab_size = 5 output_dim = 3 W = np.random.rand(vocab_size, output_dim) layer_test( embeddings.Embedding(vocab_size, output_dim, initial_weights=[W]), [[0, 1, 2, 3, 4]], [W]) # test dropout, just test if it can pass... layer_test(embeddings.Embedding(vocab_size, output_dim, dropout=0.2), [[0, 1, 2, 3, 4]], test_serialization=False) # test Embedding's support of mask input1 = Input(5, dtype='int32', mask_value=0) emb_oup = embeddings.Embedding(vocab_size, output_dim)(input1) assert emb_oup.tensor._keraflow_mask is not None
def test_TimeDistributed(): W = np.array([[1, 2]]) b = np.array([3, 4]) dense = core.Dense(2, initial_weights=[W, b]) exp_output = [] for o_slice in origin: exp_output.append(np.dot([o_slice], W)+b) exp_output = np.concatenate(exp_output, axis=0) layer_test(wrappers.TimeDistributed(dense), origin, exp_output) # test undetermined input length model = Sequential() model.add(Input(None, dtype='int32')) model.add(Embedding(4, 1, initial_weights=origin.reshape(-1,1))) model.add(wrappers.TimeDistributed(dense)) model.compile('sgd', 'mse') assert_allclose(model.predict([[0,1],[2,3]]), exp_output)
def test_highway(): W = np.array([[1, 2], [3, 4]]) b = np.array([3, 4]) W_carry = np.array([[5, 6], [7, 8]]) b_carry = np.array([3, 4]) np_input = [1, 1] def np_highway(bias): y = np.dot(np_input, W_carry) if bias: y += b_carry transform_weight = 1/(1+np.exp(-y)) output = np.dot(np_input, W) if bias: output += b return output*transform_weight+(1-transform_weight)*np_input layer_test(core.Highway(initial_weights=[W, W_carry, b, b_carry]), [np_input], [np_highway(True)]) layer_test(core.Highway(initial_weights=[W, W_carry], bias=False), [np_input], [np_highway(False)])
def test_flatten(): layer_test(core.Flatten(), [origin], [origin.flatten()]) layer_test(core.Flatten(include_batch_dim=True), [origin], origin.flatten(), input_args=dict(batch_size=1)) with pytest.raises(KError): # batch size not specified layer_test(core.Flatten(include_batch_dim=True), [origin], origin.flatten()) with pytest.raises(KError): # input shape undetermined shape = list(origin.shape) shape[0] = None layer_test(core.Flatten(), [origin], [origin.flatten()], input_args=dict(shape=tuple(shape)))
def test_dropout(): layer_test(core.Dropout(drop_rate=0.5), [np.ones((25, 100, 100))], random_exp={'mean':1, 'max':2, 'min':0}) layer_test(core.Dropout(drop_rate=0.5), [np.ones((25, 100, 100))], [np.ones((25, 100, 100))], train_mode=False) # drop_rate must be in interval [0, 1). with pytest.raises(KError): layer_test(core.Dropout(drop_rate=-.1), [np.ones((100,100, 25))], random_exp={'mean':1, 'max':2, 'min':0})
def test_element_wise_sum(): layer_test(core.ElementWiseSum(), [[origin], [origin]], [2*origin], multi_input=True)
def test_unsampling1D(): x = np.random.rand(3,3) length = 2 layer_test(convolution.UnSampling1D(length), [x], [np.repeat(x, length, axis=0)])
def test_activation(): layer_test(core.Activation('tanh'), [origin], [np.tanh(origin)])
def test_reshape(): layer_test(core.Reshape([3, 2]), [origin], [origin.reshape([3,2])]) layer_test(core.Reshape([3, -1]), [origin], [origin.reshape([3,-1])]) layer_test(core.Reshape([3, 2, -1], include_batch_dim=True), [origin], origin.reshape([3, 2, -1]), input_args=dict(batch_size=1)) layer_test(core.Reshape([-1, 2, 1], include_batch_dim=True), [origin], origin.reshape([3, 2, -1]), input_args=dict(shape=(2,3))) with pytest.raises(KError): # Trying to change batch size while but does not specify original batch size. layer_test(core.Reshape([3, 2, -1], include_batch_dim=True), [origin], origin.reshape([3, 2, -1])) with pytest.raises(KError): # more than one unknown dimension layer_test(core.Reshape([-1, -1]), [origin], [origin]) with pytest.raises(KError): # dimension should >0 or =-1 layer_test(core.Reshape([3, -2]), [origin], [origin]) with pytest.raises(KError): # shape mismatch layer_test(core.Reshape([3, 3]), [origin], [origin])
def test_element_wise_mult(): layer_test(core.ElementWiseMult(), [[origin], [origin]], [origin**2], multi_input=True)