keras的稀疏输入测试

作者:luozhipeng   发表日期:2017-8-21  浏览:151次

test Keras sparse input:

 

from keras.models import Model
from keras.layers import Dense, Input
from keras.utils.test_utils import keras_test
from keras import backend as K
from keras.backend import theano_backend as KTH
#from keras.backend import tensorflow_backend as KTF

import scipy.sparse as sparse
import numpy as np
np.random.seed(1337)


input_dim = 16
nb_hidden = 8
nb_class = 4
batch_size = 32
nb_epoch = 5



@keras_test
def test_sparse_mlp():

 input = Input(batch_shape=(None, input_dim), sparse=True)
 
 hidden = Dense(nb_hidden, activation='relu')(input)
 hidden = Dense(nb_hidden, activation='relu')(hidden)
 predictions = Dense(nb_class, activation='sigmoid')(hidden)
 model = Model(input=[input], output=predictions)
 model.compile(loss='mse', optimizer='sgd')
 
 x = sparse.rand(batch_size, input_dim, density=0.1, format='csr')
 y = np.random.random((batch_size, nb_class))
 
 model.fit(x, y, epochs=nb_epoch)
 
 
if __name__ == '__main__':
 test_sparse_mlp()

本文固定链接: http://www.luozhipeng.com/?p=1254
转载请注明: luozhipeng 2017-8-21 于 罗志鹏的BLOG 发表

上一篇: :下一篇
评论:请安装多说插件!
返回顶部