def custom_loss(y_true, y_pre): #y_pre = K.print_tensor(y_pre, message="Value of u,ur") #print(y_pre,":y_pre") #print(y_pre[0,:,1],":y_pre[0,:,1]") bs = K.get_value(K.shape(y_pre[:, 0, 0])[0]) temp1 = K.tf.maximum(subtract([y_pre[0, :, 1], y_pre[0, :, 0]]) + 1, 0) #temp1 = K.print_tensor(temp1, message="temp1:1th batch") temp1 = K.tf.reduce_sum(temp1) #temp1 = K.print_tensor(temp1, message="temp1:1th batch") for i in range(1, bs): temp1s = K.tf.maximum( subtract([y_pre[i, :, 1], y_pre[i, :, 0]]) + 1, 0) #temp1s = K.print_tensor(temp1s, message="temp1:%dth batch"%(i+1)) temp1s = K.tf.reduce_sum(temp1s) temp1 = K.concatenate( [K.reshape(temp1, (1, i)), K.reshape(temp1s, (1, 1))]) #temp1 = K.print_tensor(temp1, message="temp1:after calc %dth batch" % (i+1)) varis = [] [varis.append(var) for var in K.tf.trainable_variables()] #for i in range(0, 5): #print(varis[i],"varis[%i]"%(i)) #varis[i] = K.print_tensor(varis[i], message="Value of varis%d"%(i)) temp2 = [] for i in range(0, 5): temp2.append(K.tf.reduce_sum(K.tf.square(varis[i]))) #print(temp2) #temp2 = K.print_tensor(temp2, message="Value of temp2") temp2 = K.tf.reduce_sum(temp2) #print(temp2) #temp2 = K.print_tensor(temp2, message="Value of temp2") temp = temp1 + (([0.00000001]) * temp2) #temp = K.print_tensor(temp, message="Value of temp1+temp2*0.00000001") #print(temp,": temp") return temp
def bi_directional_attention_fuction(self, context_repr, query_repr, dim, reverse=True): context_query_dot = ly.dot( axes=(2, 2), inputs=[context_repr, Dense(dim, activation='tanh')(query_repr)]) context_query_attention = Activation('softmax')(context_query_dot) query_to_context = ly.dot(axes=(2, 1), inputs=[context_query_attention, query_repr]) context_query_sub = ly.subtract( inputs=[context_repr, query_to_context]) context_query_sub = BatchNormalization()(context_query_sub) if reverse: query_context_dot = Permute((2, 1))(context_query_dot) query_context_attention = Activation('softmax')(query_context_dot) query_context_attention = ly.dot( axes=(2, 1), inputs=[query_context_attention, context_repr]) query_context_sub = ly.subtract( inputs=[query_repr, query_context_attention]) query_context_sub = BatchNormalization()(query_context_sub) return context_query_sub, query_context_sub else: return context_query_sub
def build_model(stateful, batch_size=None): i = Input(shape=IN_SHAPE[1:]) i = Permute((1, 3, 4, 2), batch_input_shape=IN_SHAPE)(i) R = representation_rnn() C = consciousness_rnn() G = generator_rnn() D = decoder_rnn(nb_actions) h = R(i) # Get h from R c_A, c_B, c_A_soft, c_B_soft = C(h) # Get masks c_A and c_B from C b = multiply([h, c_B], name='b') # Get b through elementwise multiplication a_hat = G([c_A, c_B, b]) # Send c_A, c_B and b to G to get a_hat a_hat = Lambda(lambda x: x[:, :-1, :], output_shape=(None, latent_dim))( a_hat) # Slice dimensions to align vectors h_A = Lambda(lambda x: x[:, 1:, :], output_shape=(None, latent_dim))( h) # Slice dimensions to align vectors c_A = Lambda(lambda x: x[:, :-1, :], output_shape=(None, latent_dim))( c_A) # Slice dimensions to align vectors h_A = multiply([h_A, c_A]) # Calculate h[A] to compare against a_hat a_hat = multiply([a_hat, c_A]) # Mask a_hat consciousness_error = subtract([a_hat, h_A]) consciousness_error = Regularize( L1L2(l1=0., l2=1. * reg_lambda), name='Consciousness_Generator_Error')(consciousness_error) b_transformed = Dense(latent_dim, activation='linear')( b) # Create a layer that attempts to make b independent from h[A] b_transformed = Lambda(lambda x: x[:, :-1, :], output_shape=(None, latent_dim))(b_transformed) b_transformed = multiply([b_transformed, c_A]) transformation_error = subtract([b_transformed, h_A]) transformation_error = Regularize( L1L2(l1=0., l2=1. * reg_lambda), name='Transformation_Error')(transformation_error) intelligence_error = concatenate([ c_A_soft, c_B_soft ]) # The more elements we choose to predict, the more "intelligent" we are intelligence_error = Flatten()(intelligence_error) intelligence_error = Regularize( LinearRegularizer(c=1. * reg_lambda), name='Intelligence_Level')(intelligence_error) x_hat = D(a_hat) x_hat = ApplyRegularization()( [x_hat, consciousness_error, transformation_error, intelligence_error]) ## Compile the model and start training CN = Model(inputs=i, outputs=[x_hat]) return CN
def create_model(d1, d2): input_tensor1 = Input(shape=(d1, d2, 3)) input_tensor2 = Input(shape=(d1, d2, 3)) input_tensor3 = Input(shape=(d1, d2, 3)) # try, except block because the kernel would not let me download the weights for the network try: base_model = Net(input_shape=(d1, d2, 3), weights='imagenet', include_top=False) # the weights of this layer will be set to ones and fixed, so that we can use it to sum up the # values of the input layer (i.e. the differences in the features for the different images) summation = Dense(1, activation='linear', kernel_initializer='ones', name='summation') # create the inputs x1 = base_model(input_tensor1) x2 = base_model(input_tensor2) x3 = base_model(input_tensor3) # Here we could also use GlobalAveragePooling or simply Flatten everything x1 = GlobalMaxPooling2D()(x1) x2 = GlobalMaxPooling2D()(x2) x3 = GlobalMaxPooling2D()(x3) # calculate something more or less proportional to the euclidean distance d1 = subtract([x1, x2]) d2 = subtract([x1, x3]) d1 = Lambda(lambda val: val**2)(d1) d2 = Lambda(lambda val: val**2)(d2) d1 = summation(d1) d2 = summation(d2) # concatenate both distances and apply softmax so we get values from 0-1 d = concatenate([d1, d2]) d = Activation('softmax')(d) # build the model and show a summary model = Model(inputs=[input_tensor1, input_tensor2, input_tensor3], outputs=d) model.summary() # draw the network (it looks quite nice) from keras.utils.vis_utils import plot_model as plot plot(model, to_file='Triplet_Dense121.png') # fix the weights of the summation layer (since the weights of this layer # are shared we could also leave them trainable to get a weighted sum) for l in model.layers: if l.name == 'summation': print('fixing weights of summation layer') l.trainable = False # compile model model.compile(optimizer='sgd', loss='categorical_crossentropy') return model except: return None
def tf_2d_normal(x1, x2, mu1, mu2, s1, s2, rho): """Returns result of eq # 24 of http://arxiv.org/abs/1308.0850.""" norm1 = subtract([x1, mu1]) norm2 = subtract([x2, mu2]) s1s2 = multiply([s1, s2]) # eq 25 z = ( K.square(tf.divide(norm1, s1)) + K.square(tf.divide(norm2, s2)) - 2 * tf.divide(multiply([rho, multiply([norm1, norm2])]), s1s2)) neg_rho = 1 - K.square(rho) result = K.exp(tf.divide(-z, 2 * neg_rho)) denom = 2 * np.pi * multiply([s1s2, K.sqrt(neg_rho)]) result = tf.divide(result, denom) return result
def create_network(word_embedding, input_length): # lstm_network = create_lstm_base(word_embedding) lstm_network = Sequential(layers=[ Embedding(word_embedding.vocabulary_size, word_embedding.dimensions, weights=[word_embedding.embedding_matrix], trainable=True, mask_zero=False), BatchNormalization(axis=2), Bidirectional(LSTM(256, return_sequences=False)), ]) question1_input = Input(shape=(input_length, ), name='question1_input') question2_input = Input(shape=(input_length, ), name='question2_input') question1_lstm = lstm_network(question1_input) question2_lstm = lstm_network(question2_input) substract_questions = subtract([question1_lstm, question2_lstm]) multiply_questions = multiply([question1_lstm, question2_lstm]) dot_questinons = dot([question1_lstm, question2_lstm], axes=1, normalize=True) merged = concatenate( [substract_questions, multiply_questions, dot_questinons]) # # Attention # q1_aligned, q2_aligned = soft_attention_alignment(question1_lstm, question2_lstm) # # # Compose # q1_combined = Concatenate()([question1_lstm, q2_aligned, submult(question1_lstm, q2_aligned)]) # q2_combined = Concatenate()([question2_lstm, q1_aligned, submult(question2_lstm, q1_aligned)]) # # compose = Bidirectional(LSTM(256, return_sequences=True)) # q1_compare = compose(q1_combined) # q2_compare = compose(q2_combined) # # # Aggregate # q1_rep = apply_multiple(q1_compare, [GlobalAvgPool1D(), GlobalMaxPool1D()]) # q2_rep = apply_multiple(q2_compare, [GlobalAvgPool1D(), GlobalMaxPool1D()]) # # # Classifier # merged = Concatenate()([q1_rep, q2_rep]) dense = BatchNormalization()(merged) dense = Dense(128, activation='relu')(dense) dense = BatchNormalization()(dense) dense = Dropout(0.4)(dense) dense = Dense(128, activation='relu')(dense) dense = BatchNormalization()(dense) dense = Dropout(0.4)(dense) out_ = Dense(1, activation='sigmoid')(dense) model = Model(inputs=[question1_input, question2_input], outputs=out_) model.compile(optimizer=Adam(lr=1e-3), loss='binary_crossentropy', metrics=['binary_crossentropy', 'accuracy']) return model
def buildModel_RNN_layer2(word_index, embeddings_index, nClasses, MAX_SEQUENCE_LENGTH, EMBEDDING_DIM): # construct model model = load_model('best_model_1_sub.h5', custom_objects={'keras': keras}) model1 = Model(inputs=model.input, outputs=model.get_layer('lambda_1').output) model2 = Model(inputs=model.input, outputs=model.get_layer('lambda_2').output) for layer in model1.layers: layer.trainable = False for layer in model2.layers: layer.trainable = False output1 = model1.output output2 = model2.output sent_feat_x1 = layers.Dense(200, activation='tanh')(output1) sent_feat_x1 = layers.Dropout(rate=0.2)(sent_feat_x1) sent_feat_x2 = layers.Dense(200, activation='tanh')(output2) sent_feat_x2 = layers.Dropout(rate=0.2)(sent_feat_x2) merged = layers.subtract([sent_feat_x1, sent_feat_x2]) output = layers.Dense(1, activation='sigmoid')(merged) print("class:" , nClasses) self_model = Model(model1.inputs, output) self_model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['acc']) self_model.summary() return self_model
def build_model(self,data_shape,print_model=True): input_pos = Input(shape=(data_shape[1],data_shape[2],), name="input_pos") input_neg = Input(shape=(data_shape[1],data_shape[2],), name="input_neg") #comb=subtract([input_pos,input_neg]) user_vec=self.deep_learning_component(input_pos,data_shape,model=model_mode) #user_vec=Dropout(0.25)(user_vec) user_vec_d3 = Lambda(lambda x: K.reshape(x, (-1, 1,data_shape[2])), name = "user_vec_3d")(user_vec) batch_cos_pos_3d = Lambda(self.pairwise_cos_sim, name="batch_cos_pos_3d")([user_vec_d3,input_pos]) batch_cos_neg_3d = Lambda(self.pairwise_cos_sim, name="batch_cos_neg_3d")([user_vec_d3,input_neg]) batch_cos_pos_2d = Lambda(lambda x: K.reshape(x, (-1, K.shape(x)[1])), name="batch_cos_pos_2d")(batch_cos_pos_3d) batch_cos_neg_2d = Lambda(lambda x: K.reshape(x, (-1, K.shape(x)[1])), name="batch_cos_neg_2d")(batch_cos_neg_3d) batch_cos_diff_2d = subtract([batch_cos_pos_2d, batch_cos_neg_2d], name="batch_cos_diff_2d") output = Lambda(lambda x:K.reshape(x,(-1,data_shape[1])), name="output")(batch_cos_diff_2d) #output = Lambda( lambda x: K.sum(x, axis=1), name="output_sum")(output) #output=Dense(data_shape[1],activation='relu',use_bias=False,name="leverage")(output) model = Model(inputs=[input_pos,input_neg], outputs=output) if print_model: print(model.summary()) sgd = optimizers.SGD(lr=0.3, decay=1e-6, momentum=0.9, nesterov=True) model.compile(loss='mae', optimizer='adam') return model
def create_model(self, from_save = True): left_input = Input(self.input_shape) right_input = Input(self.input_shape) # Internal model is a softmax 10-class classifier internalmodel = Sequential() internalmodel.add(GaussianNoise(0.0001, input_shape=self.input_shape)) internalmodel.add(Flatten()) internalmodel.add(Dense(64, activation='relu')) internalmodel.add(Dropout(0.5)) internalmodel.add(Dense(64, activation='relu')) internalmodel.add(Dropout(0.5)) internalmodel.add(Dense(10, activation='softmax')) sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) # TODO, compare to ADAM internalmodel.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy']) encoded_l, encoded_r = internalmodel(left_input), internalmodel(right_input) subtracted = subtract([encoded_l,encoded_r]) both = Lambda(lambda x: K.abs(x))(subtracted) prediction = Dense(1,activation='sigmoid')(both) siamese_net = Model(inputs=[left_input,right_input], outputs=prediction) # TODO, compare to ADAM siamese_net.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy']) if from_save and os.path.isfile(self.fname): siamese_net.load_weights(self.fname) self.model = siamese_net
def buildModel(self): #word net input1 = layers.Input(shape=(self.maxLen, ), name="seq1") input2 = layers.Input(shape=(self.maxLen, ), name="seq2") comEmbedding = layers.Embedding(input_dim=self.Size_Vocab, output_dim=self.embeddingSize, input_length=self.maxLen) emb1 = comEmbedding(input1) emb2 = comEmbedding(input2) comReshape = layers.Reshape((self.maxLen, self.embeddingSize, 1)) pic1 = comReshape(emb1) pic2 = comReshape(emb2) pic3 = layers.subtract([pic1, pic2]) pics = layers.Concatenate(axis=2)([pic1, pic2, pic3]) #print(K.ndim(pics)) #cnn x = ResNet50(pics) # extract net2 x = layers.Dropout(0.5)(x) predictionLayer = layers.Dense(units=2, name="label", activation="softmax")(x) self.model = models.Model(inputs=[input1, input2], outputs=[ predictionLayer, ]) self.model.compile(optimizer=optimizers.Adam(), loss={"label": losses.binary_crossentropy}) return self.model
def get_gan_network(discriminator, shape, generator, optimizer, residue): discriminator.trainable = False gan_input = Input(shape=shape) # res_input = Input(shape=shape) # x = generator(gan_input) x = Model(inputs=generator.input, outputs=generator.get_layer('add_17').output)(gan_input) x1 = Model(inputs=residue.input, outputs=residue.get_layer('conv2d_46').output)(gan_input) y = Model(inputs=residue.input, outputs=residue.get_layer('leaky_re_lu_22').output)(gan_input) diff1 = subtract([x1, y]) z = add([diff1, x]) print(z.shape) # Using 2 UpSampling Blocks for index in range(2): z = up_sampling_block(z, 3, 256, 1) z = Conv2D(filters=3, kernel_size=9, strides=1, padding="same")(z) z = Activation('tanh')(z) gan_output = discriminator(z) gan = Model(inputs=gan_input, outputs=[z, gan_output]) gan.compile(loss=[vgg_loss, "binary_crossentropy"], loss_weights=[1., 1e-3], metrics=[PSNR, 'accuracy'], optimizer=optimizer) return gan
def model_sswe_u(window_size, vocab_size, embedding_size): input_original = Input(shape=(window_size, ), dtype='int32', name='input_original') input_corrupt = Input(shape=(window_size, ), dtype='int32', name='input_corrupt') embedding = Embedding(output_dim=embedding_size, input_dim=vocab_size, input_length=window_size, name='embedding') concat_layer = Flatten(name='concatnate') hidden_layer1 = Dense(20, activation='tanh', name='hidden') out_layer = Dense(2, name='output') embedding_main = embedding(input_original) concat_layer_main = concat_layer(embedding_main) hidden_main = hidden_layer1(concat_layer_main) out_main = out_layer(hidden_main) embedding_corrupt = embedding(input_corrupt) concat_layer_corrupt = concat_layer(embedding_corrupt) hidden_corrupt = hidden_layer1(concat_layer_corrupt) out_corrupt = out_layer(hidden_corrupt) output = subtract([out_main, out_corrupt]) model = Model(inputs=[input_original, input_corrupt], outputs=output) return model
def distTransErrorMatrix(self, y_true_onehot, y_dist, y_pred_max_mat): from keras import backend as K from keras.layers import Lambda, multiply, add, subtract y_p1 = Lambda((lambda arg: K.cast(arg[..., 1], K.floatx())))(y_pred_max_mat) y_t0 = Lambda((lambda arg: K.cast(arg[..., 0], K.floatx())))(y_true_onehot) y_t1 = Lambda((lambda arg: K.cast(arg[..., 1], K.floatx())))(y_true_onehot) y_d = Lambda((lambda arg: K.cast(arg[..., 0], K.floatx())))(y_dist) #squash, invert distance map, multiply/exponentiate by some factor factor = 2.0 currMin = 1.0 / (factor + 1.0) currMax = 1.0 newMin = 1.0 newMax = currMin y_d_squashed = Lambda(lambda arg: (arg / (arg + (factor * K.ones_like(arg)) + K.epsilon())))(y_d) y_d_invert = Lambda(lambda arg: ((((newMax - newMin) * (arg - currMin)) / (currMax - currMin)) + newMin))(y_d_squashed) y_d = Lambda(lambda arg: (arg * (factor + 1.0)))(y_d_invert) # Interior error multLayer = multiply([y_t1, y_p1]) subtractLayer = subtract([y_t1, multLayer]) interiorError = multiply([y_d, subtractLayer]) # Exterior error multLayer2 = multiply([y_t0, y_p1]) exteriorError = multiply([y_d, multLayer2]) # Total error error = add([interiorError, exteriorError]) #add one to error to ensure that aren't multiplying by zero error = Lambda(lambda arg: (K.ones_like(arg) + arg))(error) return error
def generate_siamese_model(): input_imga = Input(shape=(128, 128, 3)) input_imgb = Input(shape=(128, 128, 3)) conv_1aa = Conv2D(32, (3, 3), activation='relu') conv_1ab = Conv2D(32, (3, 3), activation='relu') mxpool_1aa = MaxPooling2D((2, 2)) mxpool_1ab = MaxPooling2D((2, 2)) conv_2a = Conv2D(64, (3, 3), activation='relu') conv_2ab = Conv2D(64, (3, 3), activation='relu') mxpool_2a = MaxPooling2D((2, 2)) mxpool_2ab = MaxPooling2D((2, 2)) conv_3a = Conv2D(128, (3, 3), activation='relu') conv_3ab = Conv2D(128, (3, 3), activation='relu') mxpool_3a = MaxPooling2D((2, 2)) mxpool_3ab = MaxPooling2D((2, 2)) conv_4a = Conv2D(128, (3, 3), activation='relu') conv_4ab = Conv2D(128, (3, 3), activation='relu') mxpool_4a = MaxPooling2D((2, 2)) mxpool_4ab = MaxPooling2D((2, 2)) flata = Flatten() flatb = Flatten() layer_1a = conv_1aa(input_imga) layer_1b = conv_1ab(input_imgb) layer_2a = mxpool_1aa(layer_1a) layer_2b = mxpool_1ab(layer_1b) layer_3a = conv_2a(layer_2a) layer_3b = conv_2ab(layer_2b) layer_4a = mxpool_2a(layer_3a) layer_4b = mxpool_2ab(layer_3b) layer_5a = conv_3a(layer_4a) layer_5b = conv_3ab(layer_4b) layer_6a = mxpool_3a(layer_5a) layer_6b = mxpool_3ab(layer_5b) layer_8a = flata(layer_6a) layer_8b = flatb(layer_6b) concat = subtract([layer_8a, layer_8b]) dropout = Dropout(0.5)(concat) dense_1 = Dense(1000, activation='relu')(dropout) dropout = Dropout(0.5)(concat) dense_1 = Dense(512, activation='relu')(dropout) dense_2 = Dense(4, activation='softmax')(dense_1) model = Model(inputs=[input_imga, input_imgb], outputs=dense_2) return model
def morphnet(in_mnist=(height, width, 1), scale=1): img = Input(shape=in_mnist, name='mnist') x1 = Erossion(filters = 16, kernel_size = (3,3), strides = (1,1), operation='e')(img) x1 = Activation('relu')(x1) x1 = Erossion(filters = 32, kernel_size = (3,3), strides=(1, 1), operation='e')(x1) x1 = Activation('relu')(x1) x2 = dilate(filters = 16,kernel_size = (3,3), strides = (1,1), operation='d')(img) x2 = Activation('relu')(x2) x2 = dilate(filters = 32, kernel_size = (3,3), strides = (1,1), operation='d')(x2) x2 = Activation('relu')(x2) res = subtract([x2,x1]) res = MaxPooling2D(pool_size = (2,2), strides = (2,2))(res) res = MaxPooling2D(pool_size = (2,2), strides = (2,2))(res) m = Flatten()(res) m = Dense(units=512,activation='relu')(m) m = Dropout(0.5)(m) out = Dense(units=10, activation='softmax')(m) model = Model(inputs=img,outputs=out) opt = keras.optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False) model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy']) model.summary() plot_model(model, to_file='model.png') return model
def create_model(self): inputs = [] field_embeddings = [] for feature in self.sparse_features: input = Input(shape=(1, ), name=feature) inputs.append(input) field_embedding = Embedding( input_dim=self.features_num_dict[feature], output_dim=self.k)(input) field_embeddings.append(field_embedding) ## fm part 0.5 * [(vi*xi)2 - vi2 * xi2] keras_squere = Lambda(lambda x: K.square(x)) vixi_squere = keras_squere(add(field_embeddings)) vi_squere_xi_squere = add([keras_squere(x) for x in field_embeddings]) y_fm = subtract([vixi_squere, vi_squere_xi_squere]) y_fm = Reshape((self.k, ))(y_fm) fc_input = BatchNormalization()(y_fm) ## fc part for n in self.hidden_layer: fc_input = Dense(n)(fc_input) fc_input = BatchNormalization()(fc_input) fc_input = Activation('relu')(fc_input) fc_input = Reshape((self.hidden_layer[-1], ))(fc_input) lr_input = Dense(1)(fc_input) y_predict = Activation('sigmoid', name='y_predict')(lr_input) model = Model(inputs=inputs, outputs=y_predict) return model
def test_merge_subtract(): i1 = layers.Input(shape=(4, 5)) i2 = layers.Input(shape=(4, 5)) i3 = layers.Input(shape=(4, 5)) i4 = layers.Input(shape=(3, 5)) o = layers.subtract([i1, i2]) assert o._keras_shape == (None, 4, 5) model = models.Model([i1, i2], o) subtract_layer = layers.Subtract() o2 = subtract_layer([i1, i2]) assert subtract_layer.output_shape == (None, 4, 5) x1 = np.random.random((2, 4, 5)) x2 = np.random.random((2, 4, 5)) out = model.predict([x1, x2]) assert out.shape == (2, 4, 5) assert_allclose(out, x1 - x2, atol=1e-4) assert subtract_layer.compute_mask([i1, i2], [None, None]) is None assert np.all(K.eval(subtract_layer.compute_mask( [i1, i2], [K.variable(x1), K.variable(x2)]))) # Test invalid use case with pytest.raises(ValueError): subtract_layer.compute_mask([i1, i2], x1) with pytest.raises(ValueError): subtract_layer.compute_mask(i1, [None, None]) with pytest.raises(ValueError): subtract_layer([i1, i2, i3]) with pytest.raises(ValueError): subtract_layer([i1])
def SiameseModel(base_model_path): # siamese_model = load_model_from_file(data_folder + 'siamese_model_.json') # siamese_model.load_weights(data_folder + '2nd_phase_siamese_weights.h5', by_name=False) siamese_model = load_model(base_model_path) siamese_model.get_layer('dense_1').activation = K.sigmoid short_xception_model = Model( inputs=siamese_model.input, outputs=siamese_model.get_layer('dense_1').output) short_xception_model = set_trainables(short_xception_model, ('dense_1')) # print(short_xception_model.summary()) left_input = Input(input_shape) right_input = Input(input_shape) with tf.variable_scope(tf.get_variable_scope(), reuse=True): encoded_l = short_xception_model(left_input) encoded_r = short_xception_model(right_input) difference = layers.subtract([encoded_l, encoded_r]) merged = layers.multiply([difference, difference]) prediction = Dense(1, activation='sigmoid', name='prediction_dense', trainable=True)(merged) siamese_net = Model(inputs=[left_input, right_input], outputs=prediction) return siamese_net
def build_tpe(n_in, n_out, weights=None): a = Input(shape=(n_in, )) p = Input(shape=(n_in, )) n = Input(shape=(n_in, )) if weights is None: weights = np.zeros((n_in, n_out)) base_model = Sequential() base_model.add( Dense(n_out, input_dim=n_in, use_bias=False, weights=[weights], activation='linear')) base_model.add(Lambda(lambda x: K.l2_normalize(x, axis=1))) a_emb = base_model(a) p_emb = base_model(p) n_emb = base_model(n) e = K.sum(multiply([a_emb, subtract([p_emb, n_emb])]), axis=1) model = Model([a, p, n], e) predict = Model(a, a_emb) model.compile(loss=triplet_loss, optimizer='rmsprop') return model, predict
def test_merge_subtract(): i1 = layers.Input(shape=(4, 5)) i2 = layers.Input(shape=(4, 5)) i3 = layers.Input(shape=(4, 5)) i4 = layers.Input(shape=(3, 5)) o = layers.subtract([i1, i2]) assert o._keras_shape == (None, 4, 5) model = models.Model([i1, i2], o) subtract_layer = layers.Subtract() o2 = subtract_layer([i1, i2]) assert subtract_layer.output_shape == (None, 4, 5) x1 = np.random.random((2, 4, 5)) x2 = np.random.random((2, 4, 5)) out = model.predict([x1, x2]) assert out.shape == (2, 4, 5) assert_allclose(out, x1 - x2, atol=1e-4) assert subtract_layer.compute_mask([i1, i2], [None, None]) is None assert np.all( K.eval( subtract_layer.compute_mask( [i1, i2], [K.variable(x1), K.variable(x2)]))) # Test invalid use case with pytest.raises(ValueError): subtract_layer.compute_mask([i1, i2], x1) with pytest.raises(ValueError): subtract_layer.compute_mask(i1, [None, None]) with pytest.raises(ValueError): subtract_layer([i1, i2, i3]) with pytest.raises(ValueError): subtract_layer([i1])
def run_bonus_rnn(batch_size=12, epochs=100, latent_dim=16): input_layer = Input(shape=(X.shape[1:])) lstm = LSTM(latent_dim)(input_layer) gru = GRU(latent_dim)(input_layer) model1 = subtract([lstm, gru]) model2 = add([lstm, gru]) #_merged = concatenate([lstm,_lstm],axis=0) dense1 = Dense(2 * latent_dim, activation='tanh')(model1) dense2 = Dense(2 * latent_dim, activation='relu')(model2) dense = add([dense1, dense2]) pred = Dense(len(gesture_sets), activation='softmax')(dense) model = Model(inputs=input_layer, outputs=pred) model.compile(loss="sparse_categorical_crossentropy", optimizer='adam', metrics=["acc"]) fit = model.fit(X, Y, epochs=epochs, batch_size=batch_size, verbose=1, validation_split=0.3, shuffle=True) return fit.history
def get_model(): ######################################## SIAMESE NETWORK ################################ #the CNN part of the SNN input = Input(input_shape) x = Conv2D(16, (3, 3), padding='same', activation='relu')(input) x = Conv2D(16, (3, 3), padding='same', activation='relu')(x) x = MaxPooling2D((2, 2), strides=(2, 2))(x) out = Flatten()(x) #encoded model that will be returned enc_model = Model(input, out) left_input = Input(input_shape) right_input = Input(input_shape) encoded_l = enc_model(left_input) encoded_r = enc_model(right_input) #distance formula and final FC layer dist = subtract([encoded_l, encoded_r]) x = Dense(32, activation='relu')(dist) x = BatchNormalization()(x) x = Dropout(0.7)(x) x = Dense(16, activation='relu')(x) prediction = Dense(2, activation='softmax')(x) siamese_net = Model(inputs=[left_input, right_input], outputs=prediction) siamese_net.compile(loss="binary_crossentropy", optimizer='adam', metrics=['accuracy']) return siamese_net, enc_model
def DownBlock(x, num_filter, kernel_size=8, stride=4, padding=2, activation=LeakyReLU): down_conv1 = Conv2D(filters=num_filter, kernel_size=kernel_size, strides=stride, padding=padding) down_conv2 = Conv2DTranspose(filters=num_filter, kernel_size=kernel_size, strides=stride, padding=padding) down_conv3 = Conv2D(filters=num_filter, kernel_size=kernel_size, strides=stride, padding=padding) l0 = activation()(down_conv1(x)) h0 = activation()(down_conv2(l0)) l1 = activation()(down_conv3(subtract([h0, x]))) return add([l1, l0])
def subNode(self, inputNode1, inputNode2, name=""): from keras import layers if inputNode1.shape() != inputNode2.shape(): self.logger("dimensionality error with " + str(inputNode1) + " and " + str(inputNode2) + " in pytorch") return None return layers.subtract([inputNode1, inputNode2], name=name)
def enhanced_lstm(self): """Function to enhance lstm""" q1 = Input(shape=(self.max_seq_length, )) q2 = Input(shape=(self.max_seq_length, )) embedding_layer = self.embedding_layer() left_input = embedding_layer(q1) right_input = embedding_layer(q2) left_input = BatchNormalization(axis=2)(left_input) right_input = BatchNormalization(axis=2)(right_input) ###Encoding#### encode = Bidirectional(LSTM(self.embedding_dim, return_sequences=True)) left_output = encode(left_input) right_output = encode(right_input) ###Aligning### left_aligned, right_aligned = self.align(left_output, right_output) #comparing q1_combined = concatenate([ left_output, right_aligned, subtract([left_output, right_aligned]), multiply([left_output, right_aligned]) ]) q2_combined = concatenate([ right_output, left_aligned, subtract([right_output, left_aligned]), multiply([right_output, left_aligned]) ]) compare = Bidirectional(LSTM(self.embedding_dim, return_sequences=True)) q1_compare = compare(q1_combined) q2_compare = compare(q2_combined) #aggregating x = self.aggregate(q1_compare, q2_compare, dropout_rate=.1) x = Dense(1, activation='sigmoid')(x) model = Model([q1, q2], x) model.compile(loss='binary_crossentropy', optimizer=Adadelta(), metrics=['accuracy']) return model
def UpBlock(x, num_filter, kernel_size=8, stride=4, padding='same', activation=LeakyReLU): up_conv1 = Conv2DTranspose(filters=num_filter, kernel_size=kernel_size, strides=stride, padding=padding) up_conv2 = Conv2D(filters=num_filter, kernel_size=kernel_size, strides=stride, padding=padding) up_conv3 = Conv2DTranspose(filters=num_filter, kernel_size=kernel_size, strides=stride, padding=padding) h0 = activation()(up_conv1(x)) l0 = activation()(up_conv2(h0)) h1 = activation()(up_conv3(subtract([l0, x]))) return add([h1, h0])
def _build_many_to_one_dict_model(self, shape, user_length=None, model_type='GRU', init_rnn_by_avg=False, neg_sampling=False, embedding_matrix=None): self.logging.info('******** model setting *********') self.logging.info('*****') self.logging.info('***** model:{}'.format(model_type)) self.logging.info('***** init :{}'.format(init_rnn_by_avg)) self.logging.info('***** negs :{}'.format(neg_sampling)) self.logging.info('*****') self.logging.info('********************************') doc_length = shape[1] doc_size = embedding_matrix.shape[0] vector_dim = embedding_matrix.shape[1] if not user_length: user_length=doc_length input_pos_ids = Input(shape=(doc_length,), dtype='int32', name="input_pos_ids") input_neg_ids = Input(shape=(doc_length,), dtype='int32', name="input_neg_ids") input_user_ids = Input(shape=(user_length,), dtype='int32', name="input_user_ids") e_layer = Embedding(doc_size,vector_dim, weights=[embedding_matrix], input_length=doc_length, trainable=False, name='embedding_history') e_layer_user = Embedding(doc_size,vector_dim, weights=[embedding_matrix], input_length=user_length, trainable=False, name='embedding_user') input_pos = e_layer(input_pos_ids) input_neg = e_layer(input_neg_ids) input_user = e_layer_user(input_user_ids) input_init = Lambda(lambda x:K.mean(x,axis=1,keepdims=False),name='input_init')(input_user) if model_type == 'GRU': rnn = GRU(units=vector_dim, input_shape=(user_length,vector_dim), name="rnn") if init_rnn_by_avg: rnn = rnn(inputs=input_user, initial_state=input_init) else: rnn = rnn(inputs=input_user) else: #LSTM rnn = LSTM(units=vector_dim, input_shape=(user_length,vector_dim), name="rnn") if init_rnn_by_avg: rnn = rnn(inputs=input_user, initial_state=[input_init,input_init]) else: rnn = rnn(inputs=input_user) user_vec = Dense(vector_dim, name="user_vec")(rnn) user_vec_d3 = Lambda(lambda x: K.expand_dims(x, axis=1), name = "user_vec_3d")(user_vec) batch_cos_pos_3d = Lambda(self.pairwise_cos_sim, name="batch_cos_pos_3d")([input_pos,user_vec_d3]) batch_cos_pos_2d = Lambda(lambda x: K.squeeze(x, axis=-1), name="batch_cos_pos_2d")(batch_cos_pos_3d) if neg_sampling: batch_cos_neg_3d = Lambda(self.pairwise_cos_sim, name="batch_cos_neg_3d")([input_neg,user_vec_d3]) batch_cos_neg_2d = Lambda(lambda x: K.squeeze(x, axis=-1), name="batch_cos_neg_2d")(batch_cos_neg_3d) batch_cos_diff_2d = subtract([batch_cos_pos_2d, batch_cos_neg_2d], name="batch_cos_diff_2d") output = Lambda(lambda x:1/(1 + K.exp(-x)), name="output")(batch_cos_diff_2d) else: output = Lambda(lambda x:1/(1 + K.exp(-x*2)), name="output")(batch_cos_pos_2d) model = Model(inputs=[input_user_ids,input_pos_ids,input_neg_ids], outputs=output) model.compile(loss='mse', optimizer="adam") self.logging.info(model.summary()) return model
def finite_difference(input_feature): channel = input_feature._keras_shape[-1] finite_feature = layers.concatenate([ K.expand_dims(K.abs( layers.subtract([input_feature[..., i + 1], input_feature[..., i] ])), axis=-1) for i in range(channel - 1) ], axis=-1) return finite_feature
def create_base_cnn_model_with_kernels_with_label(input_shape, kernels=[3, 3, 3, 3], optimizer="adamax", droprate=0.25, factor=1, num_classes=10): # Encoder input_img = Input(shape=(28, 28, 1)) input_label = Input(shape=(1, ), dtype='int32') label_embedding = Flatten()(Embedding(num_classes, 128)(input_label)) outs = [] for kernel in kernels: x = Conv2D(32, (kernel, kernel), activation='relu', padding='same')(input_img) x = Conv2D(32, (kernel, kernel), activation='relu', padding='same')(x) x = MaxPooling2D(pool_size=(2, 2))(x) x = BatchNormalization()(x) if droprate > 0: x = Dropout(droprate)(x) x = Conv2D(64 * factor, (3, 3), activation='relu', padding='same')(x) x = Conv2D(64 * factor, (3, 3), activation='relu', padding='same')(x) x = MaxPooling2D(pool_size=(2, 2))(x) x = BatchNormalization()(x) if droprate > 0: x = Dropout(droprate)(x) x = Flatten()(x) x = Dense(128, activation='relu')(x) outs.append(x) if (len(outs) > 1): x = add(outs) else: x = outs[0] mul = multiply([x, label_embedding]) sub = subtract([x, label_embedding]) add_ = add([x, label_embedding]) x = add([mul, sub, add_]) x = BatchNormalization()(x) if droprate > 0: x = Dropout(droprate)(x) output = Dense(num_classes, activation='softmax')(x) model = Model([input_img, input_label], output) model.compile(loss=keras.losses.categorical_crossentropy, optimizer=optimizer, metrics=['accuracy']) return model
def convexity_cue(contour, i): # boundary?? v1 = tf.squeeze(contour[:, :, i, :]) v0 = tf.squeeze(contour[:, :, i - 1, :]) v2 = tf.squeeze(contour[:, :, i + 1, :]) v01 = subtract([v1, v0]) v21 = subtract([v2, v1]) zero = tf.constant([[0]], dtype=tf.float32) v01 = tf.reshape(tf.concat([v01, zero], axis=0), [3]) v21 = tf.reshape(tf.concat([v21, zero], axis=0), [3]) sign = tf.sign(tf.cross(v01, v21)[2]) theta = tf.acos(cos) convexity = multiply([sign, subtract(pi, theta)]) return convexity
def __init__(self, num_users, num_items, num_previous_items, num_layers, num_units, activation_type, use_sparse_representation=False): self.num_users = num_users self.num_items = num_items self.num_previous_items = num_previous_items self.num_layers = num_layers self.num_units = num_units self.activation_type = activation_type self.use_sparse_representation = True if use_sparse_representation: concatenated_inputs_left = Input(shape=(self.num_items*(1+num_previous_items),), sparse=True) concatenated_inputs_right = Input(shape=(self.num_items*(1+num_previous_items),), sparse=True) else: past_item_input_list = [] for i in range(num_previous_items): past_item_input_list.append(Input(shape=(self.num_items,))) left_network_item_input = Input(shape=(self.num_items,)) right_network_item_input = Input(shape=(self.num_items,)) #Concatenate all of the past item inputs into a single tensor if num_previous_items > 1: past_item_inputs = concatenate(past_item_input_list) else: past_item_inputs = past_item_input_list[0] user_input = Input(shape=(self.num_users,),) #Create the shared network shared_net = self.siamese_net_half(self.num_layers, self.num_units, self.activation_type, self.use_sparse_representation) #Create the left network if use_sparse_representation: left_network = shared_net.half_net(concatenated_inputs_left) else: left_network = shared_net.half_net([user_input, past_item_inputs, left_network_item_input]) #Create the right network as a shared set of layers if use_sparse_representation: right_network = shared_net.half_net(concatenated_inputs_right) else: right_network = shared_net.half_net([user_input, past_item_inputs, right_network_item_input]) #Merge the two halves output = subtract([left_network, right_network]) #Create the model if use_sparse_representation: self.model = Model(inputs=[concatenated_inputs_left, concatenated_inputs_right], outputs=output) else: input_list = [x for x in past_item_input_list] input_list.extend([user_input, left_network_item_input, right_network_item_input]) self.model = Model(inputs=input_list, outputs=output)