def get_outputs(X_batch): l2_reg = 0.01 stim_shape = (numTime, 40, 50, 50) RMSmod = RMSprop(lr=0.001, rho=0.99, epsilon=1e-6) num_filters = (8, 16) filter_size = (13, 13) weight_init = 'he_normal' batchsize = 100 model = Sequential() # first convolutional layer model.add(TimeDistributedConvolution2D(num_filters[0], filter_size[0], filter_size[1], input_shape=stim_shape, border_mode='same', subsample=(1,1), W_regularizer=l2(l2_reg))) #Add relu activation separately for threshold visualizations model.add(Activation('relu')) # max pooling layer model.add(TimeDistributedMaxPooling2D(pool_size=(2, 2), ignore_border=True)) # flatten model.add(TimeDistributedFlatten()) # Add dense (affine) layer with relu activation model.add(TimeDistributedDense(num_filters[1], W_regularizer=l2(l2_reg), activation='relu')) # Add LSTM, forget gate bias automatically initialized to 1, default weight initializations recommended model.add(LSTM(100*num_filters[1], return_sequences=True)) # # Add a final dense (affine) layer with softplus activation model.add(TimeDistributedDense(1, init=weight_init, W_regularizer=l2(l2_reg), activation='softplus')) model.compile(loss='poisson_loss', optimizer=RMSmod) model.load_weights(weights_dir) if not memories: get_outputs = theano.function([model.layers[0].input], model.layers[5].get_output(train=False)) outputs = get_outputs(X_batch) else: model2 = Sequential() model2.add(TimeDistributedConvolution2D(num_filters[0], filter_size[0], filter_size[1], input_shape=stim_shape, weights=model.layers[0].get_weights(), border_mode='same', subsample=(1,1), W_regularizer=l2(l2_reg))) #Add relu activation separately for threshold visualizations model2.add(Activation('relu')) # max pooling layer model2.add(TimeDistributedMaxPooling2D(pool_size=(2, 2), ignore_border=True)) # flatten model2.add(TimeDistributedFlatten()) # Add dense (affine) layer with relu activation model2.add(TimeDistributedDense(num_filters[1], weights=model.layers[4].get_weights(), W_regularizer=l2(l2_reg), activation='relu')) # Add LSTM, forget gate bias automatically initialized to 1, default weight initializations recommended model2.add(LSTMMem(100*num_filters[1], weights=model.layers[5].get_weights(), return_memories=True)) model2.compile(loss='poisson_loss', optimizer=RMSmod) get_outputs = theano.function([model2.layers[0].input], model2.layers[5].get_output(train=False)) outputs = get_outputs(X_batch) return outputs
def __init__(self, embedding_mat=None, maxlen_doc=7, maxlen_sent=50, filter_length=[3, 4, 5, 6], nb_filters=200, n_vocab=10000, embedding_dims=300, hidden_gru=64, n_classes=5): if embedding_mat is not None: self.n_vocab, self.embedding_dims = embedding_mat.shape else: self.n_vocab = n_vocab self.embedding_dims = embedding_dims self.maxlen_doc = maxlen_doc self.maxlen_sent = maxlen_sent self.filter_length = filter_length self.nb_filters = nb_filters self.hidden_gru = hidden_gru print "Building the model" #graph model model=Graph() model.add_input(name='input', input_shape=(self.maxlen_doc*self.maxlen_sent,), dtype='int') #Model embedding layer, for word index-> word embedding transformation model.add_node(Embedding(self.n_vocab, self.embedding_dims, weights=[self.embedding_mat], input_length=self.maxlen_sent*self.maxlen_doc), name='embedding', input='input') model.add_node(Reshape((self.maxlen_doc, 1, self.maxlen_sent, self.embedding_dims)), name='reshape_5d', input='embedding') #define the different filters conv_layer = [] for each_length in filter_length: model.add_node(TimeDistributedConvolution2D(self.nb_filters/len(filter_length), each_length, self.embedding_dims, border_mode='valid', input_shape=(self.maxlen_doc,1,self.maxlen_sent, self.embedding_dims)), name='conv_{}'.format(each_length), input='reshape_5d') model.add_node(Activation('relu'), name='relu_conv_{}'.format(each_length), input='conv_{}'.format(each_length)) model.add_node(TimeDistributedMaxPooling2D(pool_size=(int(self.maxlen_sent - each_length+1), 1), border_mode='valid'), name='pool_conv_{}'.format(each_length), input='relu_conv_{}'.format(each_length)) model.add_node(TimeDistributedFlatten(), name='flatten_conv_{}'.format(each_length), input='pool_conv_{}'.format(each_length)) conv_layer.append('flatten_conv_{}'.format(each_length)) # model.add_node(Activation('relu'), name='relu', inputs=conv_layer) print conv_layer model.add_node(GRU(self.hidden_gru), name='gru_forward', inputs=conv_layer) model.add_node(GRU(self.hidden_gru, go_backwards=True), name='gru_backward', inputs=conv_layer) model.add_node(Dropout(0.5), name='gru_outputs', inputs=['gru_forward', 'gru_backward']) model.add_node(Dense(n_classes), name='full_con', input='gru_outputs') model.add_node(Activation('softmax'), name='prob', input='full_con') model.add_output(name='pred', input='prob') model.compile('rmsprop', loss = {'pred': 'categorical_crossentropy'})
features, labels = helperFuncs.getTargets( "justbonds") #get the target vector for each CID outsize = helperFuncs.getOutSize(features) fakeImage = np.ones((size, size)) test = im2window(fakeImage, wSize, stride) timeSteps = test.shape[0] print timeSteps """DEFINE THE MODEL HERE""" model = Sequential() model.add( TimeDistributedConvolution2D(8, 4, 4, border_mode='valid', input_shape=(timeSteps, 1, wSize, wSize))) model.add(TimeDistributedMaxPooling2D(pool_size=(2, 2), border_mode='valid')) model.add(Activation('relu')) model.add(TimeDistributedConvolution2D(16, 3, 3, border_mode='valid')) model.add(Activation('relu')) #model.add(TimeDistributedMaxPooling2D(pool_size=(2, 2),border_mode='valid')) model.add(TimeDistributedConvolution2D(16, 3, 3, border_mode='valid')) model.add(Activation('relu')) model.add(TimeDistributedFlatten()) model.add(Activation('relu')) model.add(GRU(output_dim=50, return_sequences=False)) #model.add(GRU(output_dim=50,return_sequences=False)) model.add(Dropout(.2)) model.add(Dense(outsize))
def VGG_16(weights_path=None): model = Sequential() model.add(ZeroPadding2D((1, 1), input_shape=(3, None, None))) #0 model.add(TimeDistributedConvolution2D(64, 3, 3, activation='relu')) #1 model.add(ZeroPadding2D((1, 1))) #2 model.add(TimeDistributedConvolution2D(64, 3, 3, activation='relu')) #3 model.add(TimeDistributedMaxPooling2D((2, 2), strides=(2, 2))) #4 model.add(ZeroPadding2D((1, 1))) #5 model.add(TimeDistributedConvolution2D(128, 3, 3, activation='relu')) #6 model.add(ZeroPadding2D((1, 1))) #7 model.add(TimeDistributedConvolution2D(128, 3, 3, activation='relu')) #8 model.add(TimeDistributedMaxPooling2D((2, 2), strides=(2, 2))) #9 model.add(ZeroPadding2D((1, 1))) #10 model.add(TimeDistributedConvolution2D(256, 3, 3, activation='relu')) #11 model.add(ZeroPadding2D((1, 1))) #12 model.add(TimeDistributedConvolution2D(256, 3, 3, activation='relu')) #13 model.add(ZeroPadding2D((1, 1))) #14 model.add(TimeDistributedConvolution2D(256, 3, 3, activation='relu')) #15 model.add(TimeDistributedMaxPooling2D((2, 2), strides=(2, 2))) #16 model.add(ZeroPadding2D((1, 1))) #17 model.add(TimeDistributedConvolution2D(512, 3, 3, activation='relu')) #18 model.add(ZeroPadding2D((1, 1))) #19 model.add(TimeDistributedConvolution2D(512, 3, 3, activation='relu')) #20 model.add(ZeroPadding2D((1, 1))) #21 model.add(TimeDistributedConvolution2D(512, 3, 3, activation='relu')) #22 model.add(TimeDistributedMaxPooling2D((1, 1), strides=(1, 1))) #23 model.add(ZeroPadding2D((1, 1))) #24 model.add(TimeDistributedConvolution2D(512, 3, 3, activation='relu')) #25 model.add(ZeroPadding2D((1, 1))) #26 model.add(TimeDistributedConvolution2D(512, 3, 3, activation='relu')) #27 model.add(ZeroPadding2D((1, 1))) #28 model.add(TimeDistributedConvolution2D(512, 3, 3, activation='relu')) #29 model.add(TimeDistributedFlatten()) #30 #model.add(MaxPooling2D((2,2), strides=(2,2))) #30 #model.add(Flatten()) #31 #model.add(Dense(4096, activation='relu')) #32 #model.add(Dropout(0.5)) #33 #model.add(Dense(4096, activation='relu')) #34 #model.add(Dropout(0.5)) #35 #model.add(Dense(1000, activation='softmax')) #36 if weights_path: model.load_weights(weights_path) return model
def __init__(self, cell_index, stimulus_type, num_timesteps=152, num_filters=(8, 16), filter_size=(13, 13), loss='poisson_loss', optimizer='adam', weight_init='normal', l2_reg=0., mean_adapt=False): """ Convolutional neural network Parameters ---------- cell_index : int Which cell to use stimulus_type : string Either 'whitenoise' or 'naturalscene' num_filters : tuple, optional Number of filters in each layer. Default: (8, 16) num_timesteps: int Timesteps the recurrent layer should keep track of. Default: 152 ~1.9 seconds w/in 1-5 sec range filter_size : tuple, optional Convolutional filter size. Default: (13, 13) loss : string or object, optional A Keras objective. Default: 'poisson_loss' optimizer : string or object, optional A Keras optimizer. Default: 'adam' weight_init : string weight initialization. Default: 'normal' l2_reg : float, optional How much l2 regularization to apply to all filter weights """ from keras.layers.extra import TimeDistributedConvolution2D, TimeDistributedMaxPooling2D, TimeDistributedFlatten self.stim_shape = (num_timesteps, 40, 50, 50) # build the model with notify('Building lstm'): self.model = Sequential() # first convolutional layer self.model.add( TimeDistributedConvolution2D(num_filters[0], filter_size[0], filter_size[1], input_shape=self.stim_shape, init=weight_init, border_mode='same', subsample=(1, 1), W_regularizer=l2(l2_reg))) #Add relu activation separately for threshold visualizations self.model.add(Activation('relu')) # max pooling layer self.model.add( TimeDistributedMaxPooling2D(pool_size=(2, 2), ignore_border=True)) # flatten self.model.add(TimeDistributedFlatten()) # Add dense (affine) layer with relu activation self.model.add( TimeDistributedDense(num_filters[1], init=weight_init, W_regularizer=l2(l2_reg))) # Add relu activation separately for threshold visualization self.model.add(Activation('relu')) # Add LSTM, forget gate bias automatically initialized to 1, default weight initializations recommended self.model.add( LSTM(100 * num_filters[1], forget_bias_init='one', return_sequences=True)) # Add a final dense (affine) layer with softplus activation self.model.add( TimeDistributedDense(1, init=weight_init, W_regularizer=l2(l2_reg), activation='softplus')) # save architecture string (for markdown file) self.architecture = '\n'.join([ '{} convolutional filters of size {}'.format( num_filters[0], filter_size), '{} filters in the second (fully connected) layer'.format( num_filters[1]), 'weight initialization: {}'.format(weight_init), 'l2 regularization: {}'.format(l2_reg), 'stimulus shape: {}'.format(self.stim_shape) ]) # compile super().__init__(cell_index, stimulus_type, loss, optimizer, mean_adapt)