def layer_test(layer_cls, kwargs={}, input_shape=None, input_dtype=None, input_data=None, expected_output=None, expected_output_dtype=None, fixed_batch_size=False, supports_masking=False): # generate input data if input_data is None: if not input_shape: raise AssertionError() if not input_dtype: input_dtype = K.floatx() input_data_shape = list(input_shape) for i, e in enumerate(input_data_shape): if e is None: input_data_shape[i] = np.random.randint(1, 4) input_mask = [] if all(isinstance(e, tuple) for e in input_data_shape): input_data = [] for e in input_data_shape: input_data.append( (10 * np.random.random(e)).astype(input_dtype)) if supports_masking: a = np.full(e[:2], False) a[:, :e[1]//2] = True input_mask.append(a) else: input_data = (10 * np.random.random(input_data_shape)) input_data = input_data.astype(input_dtype) if supports_masking: a = np.full(input_data_shape[:2], False) a[:, :input_data_shape[1]//2] = True print(a) print(a.shape) input_mask.append(a) else: if input_shape is None: input_shape = input_data.shape if input_dtype is None: input_dtype = input_data.dtype if expected_output_dtype is None: expected_output_dtype = input_dtype # instantiation layer = layer_cls(**kwargs) # test get_weights , set_weights at layer level weights = layer.get_weights() layer.set_weights(weights) try: expected_output_shape = layer.compute_output_shape(input_shape) except Exception: expected_output_shape = layer._compute_output_shape(input_shape) # test in functional API if isinstance(input_shape, list): if fixed_batch_size: x = [Input(batch_shape=e, dtype=input_dtype) for e in input_shape] if supports_masking: mask = [Input(batch_shape=e[0:2], dtype=bool) for e in input_shape] else: x = [Input(shape=e[1:], dtype=input_dtype) for e in input_shape] if supports_masking: mask = [Input(shape=(e[1],), dtype=bool) for e in input_shape] else: if fixed_batch_size: x = Input(batch_shape=input_shape, dtype=input_dtype) if supports_masking: mask = Input(batch_shape=input_shape[0:2], dtype=bool) else: x = Input(shape=input_shape[1:], dtype=input_dtype) if supports_masking: mask = Input(shape=(input_shape[1],), dtype=bool) if supports_masking: y = layer(Masking()(x), mask=mask) else: y = layer(x) if not (K.dtype(y) == expected_output_dtype): raise AssertionError() # check with the functional API if supports_masking: model = Model([x, mask], y) actual_output = model.predict([input_data, input_mask[0]]) else: model = Model(x, y) actual_output = model.predict(input_data) actual_output_shape = actual_output.shape for expected_dim, actual_dim in zip(expected_output_shape, actual_output_shape): if expected_dim is not None: if not (expected_dim == actual_dim): raise AssertionError() if expected_output is not None: assert_allclose(actual_output, expected_output, rtol=1e-3) # test serialization, weight setting at model level model_config = model.get_config() recovered_model = model.__class__.from_config(model_config) if model.weights: weights = model.get_weights() recovered_model.set_weights(weights) _output = recovered_model.predict(input_data) assert_allclose(_output, actual_output, rtol=1e-3) # test training mode (e.g. useful when the layer has a # different behavior at training and testing time). if has_arg(layer.call, 'training'): model.compile('rmsprop', 'mse') model.train_on_batch(input_data, actual_output) # test instantiation from layer config layer_config = layer.get_config() layer_config['batch_input_shape'] = input_shape layer = layer.__class__.from_config(layer_config) # for further checks in the caller function return actual_output
class STSTask(): def __init__(self, c): self.c = c def load_resc(self, dictname): self.embed = Embedder(dictname, self.c['wordvectdim']) def load_data(self, trainfile, validfile, testfile): self.traindata = self._load_data(trainfile) self.validdata = self._load_data(validfile) self.testdata = self._load_data(testfile) def _load_data(self, filename): global ADD_PREFIX print(filename) s0, s1, labels = [], [], [] lines = open(filename, 'r', encoding='utf8').read().splitlines() for line in lines: try: s0x, s1x, label = line.rstrip().split('\t') labels.append(float(label)) if ADD_PREFIX: prefix = '' if filename == 'Dataset/train_set.csv' or filename == 'Dataset/dev_set.csv': prefix = 'en/' else: prefix = 'es/' s0.append([ prefix + word for word in word_tokenize(s0x) if word not in string.punctuation ]) s1.append([ prefix + word for word in word_tokenize(s1x) if word not in string.punctuation ]) #s0.append([prefix+word.lower() for word in word_tokenize(s0x) if word not in string.punctuation]) #s1.append([prefix+word.lower() for word in word_tokenize(s1x) if word not in string.punctuation]) else: s0.append([ word for word in word_tokenize(s0x) if word not in string.punctuation ]) s1.append([ word for word in word_tokenize(s1x) if word not in string.punctuation ]) #s0.append([word.lower() for word in word_tokenize(s0x) if word not in string.punctuation]) #s1.append([word.lower() for word in word_tokenize(s1x) if word not in string.punctuation]) except: print('Error in line: ' + str(line)) m0 = self.embed.matrixize(s0, self.c['sentencepad']) m1 = self.embed.matrixize(s1, self.c['sentencepad']) classes = np.zeros((len(labels), self.c['num_classes'])) for i, label in enumerate(labels): if np.floor(label) + 1 < self.c['num_classes']: classes[i, int(np.floor(label)) + 1] = label - np.floor(label) classes[i, int(np.floor(label))] = np.floor(label) - label + 1 #print(classes) return { 'labels': labels, 's0': s0, 's1': s1, 'classes': classes, 'm0': m0, 'm1': m1 } def create_model(self): K.clear_session() input0 = Input(shape=(self.c['sentencepad'], self.c['wordvectdim'])) input1 = Input(shape=(self.c['sentencepad'], self.c['wordvectdim'])) Convolt_Layer = [] MaxPool_Layer = [] Flatten_Layer = [] for kernel_size, filters in self.c['cnnfilters'].items(): Convolt_Layer.append( Convolution1D(filters=filters, kernel_size=kernel_size, padding='valid', activation=self.c['cnnactivate'], kernel_initializer=self.c['cnninitial'])) MaxPool_Layer.append( MaxPooling1D(pool_size=int(self.c['sentencepad'] - kernel_size + 1))) Flatten_Layer.append(Flatten()) Convolted_tensor0 = [] Convolted_tensor1 = [] for channel in range(len(self.c['cnnfilters'])): Convolted_tensor0.append(Convolt_Layer[channel](input0)) Convolted_tensor1.append(Convolt_Layer[channel](input1)) MaxPooled_tensor0 = [] MaxPooled_tensor1 = [] for channel in range(len(self.c['cnnfilters'])): MaxPooled_tensor0.append(MaxPool_Layer[channel]( Convolted_tensor0[channel])) MaxPooled_tensor1.append(MaxPool_Layer[channel]( Convolted_tensor1[channel])) Flattened_tensor0 = [] Flattened_tensor1 = [] for channel in range(len(self.c['cnnfilters'])): Flattened_tensor0.append(Flatten_Layer[channel]( MaxPooled_tensor0[channel])) Flattened_tensor1.append(Flatten_Layer[channel]( MaxPooled_tensor1[channel])) if len(self.c['cnnfilters']) > 1: Flattened_tensor0 = concatenate(Flattened_tensor0) Flattened_tensor1 = concatenate(Flattened_tensor1) else: Flattened_tensor0 = Flattened_tensor0[0] Flattened_tensor1 = Flattened_tensor1[0] absDifference = Lambda(lambda X: K.abs(X[0] - X[1]))( [Flattened_tensor0, Flattened_tensor1]) mulDifference = multiply([Flattened_tensor0, Flattened_tensor1]) allDifference = concatenate([absDifference, mulDifference]) for ilayer, densedimension in enumerate(self.c['densedimension']): allDifference = Dense( units=int(densedimension), activation=self.c['denseactivate'], kernel_initializer=self.c['denseinitial'])(allDifference) output = Dense( name='output', units=self.c['num_classes'], activation='softmax', kernel_initializer=self.c['denseinitial'])(allDifference) self.model = Model(inputs=[input0, input1], outputs=output) self.model.compile(loss='mean_squared_error', optimizer=self.c['optimizer']) #self.model.compile(loss={'output': self._lossfunction}, optimizer=self.c['optimizer']) def _lossfunction(self, y_true, y_pred): ny_true = y_true[:, 1] #+ 2*y_true[:,2] + 3*y_true[:,3] + 4*y_true[:,4] + 5*y_true[:,5] ny_pred = y_pred[:, 1] #+ 2*y_pred[:,2] + 3*y_pred[:,3] + 4*y_pred[:,4] + 5*y_pred[:,5] my_true = K.mean(ny_true) my_pred = K.mean(ny_pred) var_true = (ny_true - my_true)**2 var_pred = (ny_pred - my_pred)**2 return -K.sum( (ny_true - my_true) * (ny_pred - my_pred), axis=-1) / (K.sqrt( K.sum(var_true, axis=-1) * K.sum(var_pred, axis=-1))) def eval_model(self): global TEXT_EVAL results = [] for data in [self.traindata, self.validdata, self.testdata]: predictionclasses = [] for dataslice, _ in self._sample_pairs(data, len(data['classes']), shuffle=False, once=True): predictionclasses += list(self.model.predict(dataslice)) #print(predictionclasses) scores = [] for p in predictionclasses: if p[0] > p[1]: scores.append(0) else: scores.append(1) #prediction = np.dot(np.array(predictionclasses),np.arange(self.c['num_classes'])) gold_scores = data['labels'] result = sklearn.metrics.log_loss(gold_scores, scores) TP, FP, TN, FN = perf_measure(gold_scores, scores) acc = np.sum( np.array(gold_scores) == np.array(scores)) / len(gold_scores) print('Log Loss: ' + str(result)) print('Acc: ' + str(acc)) print('TP: ' + str(TP) + '\tFP: ' + str(FP) + '\tTN: ' + str(TN) + '\tFN: ' + str(FN)) #print(scores) #print(gold_scores) results.append([result, acc, TP, FP, TN, FN]) #result=pearsonr(prediction, goldlabels)[0] #results.append(round(result,4)) print('TEST:' + str(results[0])) print('DEV:' + str(results[1])) print('TEST:' + str(results[2])) #print(results) return results def fit_model(self, wfname): kwargs = dict() kwargs['generator'] = self._sample_pairs(self.traindata, self.c['batch_size']) kwargs['steps_per_epoch'] = self.c['num_batchs'] kwargs['epochs'] = self.c['num_epochs'] class Evaluate(Callback): def __init__(self, task, wfname): self.task = task self.bestresult = 0.0 self.wfname = wfname def on_epoch_end(self, epoch, logs={}): results = self.task.eval_model() if results[1][0] > self.bestresult: self.bestresult = results[1][0] self.task.model.save(self.wfname) #_,validresult,_,_,_,_,_ = self.task.eval_model() #if validresult > self.bestresult: # self.bestresult = validresult # self.task.model.save(self.wfname) kwargs['callbacks'] = [Evaluate(self, wfname)] return self.model.fit_generator(verbose=2, **kwargs) def _sample_pairs(self, data, batch_size, shuffle=True, once=False): num = len(data['classes']) idN = int((num + batch_size - 1) / batch_size) ids = list(range(num)) while True: if shuffle: random.shuffle(ids) datacopy = copy.deepcopy(data) for name, value in datacopy.items(): valuer = copy.copy(value) for i in range(num): valuer[i] = value[ids[i]] datacopy[name] = valuer for i in range(idN): sl = slice(i * batch_size, (i + 1) * batch_size) dataslice = dict() for name, value in datacopy.items(): dataslice[name] = value[sl] x = [dataslice['m0'], dataslice['m1']] y = dataslice['classes'] yield (x, y) if once: break
# Make 2 days (overlap 1 day) data, _, _ = utils.overlap(data, win = 4, t = 8) if MODEL=='': print('insert model..') exit() print('Data Shape...',data.shape) ##predict data... import demo model=demo.stacked_lstm_ae(8,4096,'relu',32,'sgd',0.2,0.1) model.load_weights(MODEL_FILE) from tensorflow.python.keras.models import Model model = Model(inputs=model.inputs, outputs=model.get_layer("encoder").output) data = model.predict(data) # Reshape data = data.reshape(data.shape[1], data.shape[0]) ds = Dataset_transformations(data.T, 1000, data.shape) if os.path.exists(PREFIX+CONFIG_NAME+'.zip'): clust_obj = dataset_utils.load_single(PREFIX+CONFIG_NAME+'.zip') else: print 'Doing kmeans.....' clust_obj = Clustering(ds,n_clusters=15,n_init=100,features_first=False) clust_obj.batch_kmeans(10) print 'Saving .....' clust_obj.save(PREFIX+CONFIG_NAME+'.zip') # Descriptor num_min: 1 num_min = 1
def main(batch_size=150, p_drop=0.4, latent_dim=2, cpl_fn='minvar', cpl_str=1e-3, n_epoch=500, run_iter=0, model_id='cnn', exp_name='MNIST'): fileid = model_id + \ '_cf_' + cpl_fn + \ '_cs_' + str(cpl_str) + \ '_pd_' + str(p_drop) + \ '_bs_' + str(batch_size) + \ '_ld_' + str(latent_dim) + \ '_ne_' + str(n_epoch) + \ '_ri_' + str(run_iter) fileid = fileid.replace('.', '-') train_dat, train_lbl, val_dat, val_lbl, dir_pth = dataIO(exp_name=exp_name) #Architecture parameters ------------------------------ input_dim = train_dat.shape[1] n_arms = 2 fc_dim = 49 #Model definition ------------------------------------- M = {} M['in_ae'] = Input(shape=(28, 28, 1), name='in_ae') for i in range(n_arms): M['co1_ae_' + str(i)] = Conv2D(10, (3, 3), activation='relu', padding='same', name='co1_ae_' + str(i))(M['in_ae']) M['mp1_ae_' + str(i)] = MaxPooling2D( (2, 2), padding='same', name='mp1_ae_' + str(i))(M['co1_ae_' + str(i)]) M['dr1_ae_' + str(i)] = Dropout(rate=p_drop, name='dr1_ae_' + str(i))( M['mp1_ae_' + str(i)]) M['fl1_ae_' + str(i)] = Flatten(name='fl1_ae_' + str(i))(M['dr1_ae_' + str(i)]) M['fc01_ae_' + str(i)] = Dense(fc_dim, activation='relu', name='fc01_ae_' + str(i))(M['fl1_ae_' + str(i)]) M['fc02_ae_' + str(i)] = Dense(fc_dim, activation='relu', name='fc02_ae_' + str(i))(M['fc01_ae_' + str(i)]) M['fc03_ae_' + str(i)] = Dense(fc_dim, activation='relu', name='fc03_ae_' + str(i))(M['fc02_ae_' + str(i)]) if cpl_fn in ['mse']: M['ld_ae_' + str(i)] = Dense(latent_dim, activation='linear', name='ld_ae_' + str(i))(M['fc03_ae_' + str(i)]) elif cpl_fn in ['mseBN', 'fullcov', 'minvar']: M['fc04_ae_' + str(i)] = Dense(latent_dim, activation='linear', name='fc04_ae_' + str(i))( M['fc03_ae_' + str(i)]) M['ld_ae_' + str(i)] = BatchNormalization( scale=False, center=False, epsilon=1e-10, momentum=0.99, name='ld_ae_' + str(i))(M['fc04_ae_' + str(i)]) M['fc05_ae_' + str(i)] = Dense(fc_dim, activation='relu', name='fc05_ae_' + str(i))(M['ld_ae_' + str(i)]) M['fc06_ae_' + str(i)] = Dense(fc_dim, activation='relu', name='fc06_ae_' + str(i))(M['fc05_ae_' + str(i)]) M['fc07_ae_' + str(i)] = Dense(fc_dim * 4, activation='relu', name='fc07_ae_' + str(i))(M['fc06_ae_' + str(i)]) M['re1_ae_' + str(i)] = Reshape( (14, 14, 1), name='re1_ae_' + str(i))(M['fc07_ae_' + str(i)]) M['us1_ae_' + str(i)] = UpSampling2D( (2, 2), name='us1_ae_' + str(i))(M['re1_ae_' + str(i)]) M['co2_ae_' + str(i)] = Conv2D(10, (3, 3), activation='relu', padding='same', name='co2_ae_' + str(i))(M['us1_ae_' + str(i)]) M['ou_ae_' + str(i)] = Conv2D(1, (3, 3), activation='sigmoid', padding='same', name='ou_ae_' + str(i))(M['co2_ae_' + str(i)]) cplAE = Model(inputs=M['in_ae'], outputs=[M['ou_ae_' + str(i)] for i in range(n_arms)] + [M['ld_ae_' + str(i)] for i in range(n_arms)]) if cpl_fn in ['mse', 'mseBN']: cpl_fn_loss = mse elif cpl_fn == 'fullcov': cpl_fn_loss = fullcov elif cpl_fn == 'minvar': cpl_fn_loss = minvar assert type(cpl_fn) #Create loss dictionary loss_dict = { 'ou_ae_0': mse(M['in_ae'], M['ou_ae_0']), 'ou_ae_1': mse(M['in_ae'], M['ou_ae_1']), 'ld_ae_0': cpl_fn_loss(M['ld_ae_0'], M['ld_ae_1']), 'ld_ae_1': cpl_fn_loss(M['ld_ae_1'], M['ld_ae_0']) } #Loss weights dictionary loss_wt_dict = { 'ou_ae_0': 1.0, 'ou_ae_1': 1.0, 'ld_ae_0': cpl_str, 'ld_ae_1': cpl_str } #Add loss definitions to the model cplAE.compile(optimizer='adam', loss=loss_dict, loss_weights=loss_wt_dict) #Data feed train_input_dict = {'in_ae': train_dat} val_input_dict = {'in_ae': val_dat} train_output_dict = { 'ou_ae_0': train_dat, 'ou_ae_1': train_dat, 'ld_ae_0': np.empty((train_dat.shape[0], latent_dim)), 'ld_ae_1': np.empty((train_dat.shape[0], latent_dim)) } val_output_dict = { 'ou_ae_0': val_dat, 'ou_ae_1': val_dat, 'ld_ae_0': np.empty((val_dat.shape[0], latent_dim)), 'ld_ae_1': np.empty((val_dat.shape[0], latent_dim)) } log_cb = CSVLogger(filename=dir_pth['logs'] + fileid + '.csv') #Train model cplAE.fit(train_input_dict, train_output_dict, validation_data=(val_input_dict, val_output_dict), batch_size=batch_size, initial_epoch=0, epochs=n_epoch, verbose=2, shuffle=True, callbacks=[log_cb]) #Saving weights cplAE.save_weights(dir_pth['result'] + fileid + '-modelweights' + '.h5') matsummary = {} #Trained model prediction for i in range(n_arms): encoder = Model(inputs=M['in_ae'], outputs=M['ld_ae_' + str(i)]) matsummary['z_val_' + str(i)] = encoder.predict({'in_ae': val_dat}) matsummary['z_train_' + str(i)] = encoder.predict({'in_ae': train_dat}) matsummary['train_lbl'] = train_lbl matsummary['val_lbl'] = val_lbl sio.savemat(dir_pth['result'] + fileid + '-summary.mat', matsummary) return
loss='categorical_crossentropy', metrics=['accuracy']) # Training model2.fit(x=data.x_train, y=data.y_train, epochs=1, batch_size=128) # Evaluation result = model2.evaluate(x=data.x_test, y=data.y_test) for name, value in zip(model2.metrics_names, result): print(name, value) print("{0}: {1:.2%}".format(model2.metrics_names[1], result[1])) # Examples of Mis-Classified Images y_pred = model2.predict(x=data.x_test) cls_pred = np.argmax(y_pred, axis=1) plot_example_errors(cls_pred) # Save & Load Model path_model = '../checkpoints' if not os.path.exists(path_model): os.makedirs(path_model) print("Make folder success!") path_model = os.path.join(path_model, 'model.kears') model2.save(path_model) print("Success to write!") del model2 print("Model-3 Test!")
user_feature_columns = dnn_feature_columns[0:15] item_feature_columns = dnn_feature_columns[15:] print(user_feature_columns) print(item_feature_columns) user_feature_inputs_keys = sparse_features[0:15] print(user_feature_inputs_keys) item_feature_inputs_keys = sparse_features[15:] print(item_feature_inputs_keys) # other_feature_input_keys = dense_features # 3.Define Model,train,predict and evaluate user_vec_name = 'user_vec' item_vec_name = 'item_vec' model = DSSM4FatureColumn(user_feature_columns, item_feature_columns, user_inputs_keys=user_feature_inputs_keys, item_inputs_keys=item_feature_inputs_keys) model.compile("adam", "binary_crossentropy", metrics=['binary_crossentropy', tf.keras.metrics.AUC()], ) model.fit(train_model_input) print("=============train finish==================") # eval_result = model.evaluate(test_model_input) save_model(model, './dssm_model') user_embedding_model = Model(inputs=model.user_input, outputs=model.user_embedding) item_embedding_model = Model(inputs=model.item_input, outputs=model.item_embedding) # 重写input后的predict输入不能包含label,否则"AttributeError: 'Model' object has no attribute '_training_endpoints'" user_embs = user_embedding_model.predict(test_model_input) item_embs = item_embedding_model.predict(predict_model_input)
def train(): data = load_data() item_set = set(data['movie_id'].unique()) SEQ_LEN = 50 # 1.Label Encoding for sparse features,and process sequence features with `gen_date_set` and `gen_model_input` features = ['user_id', 'movie_id', 'gender', 'age', 'occupation', 'zip'] feature_max_idx = {} for feature in features: lbe = LabelEncoder() data[feature] = lbe.fit_transform(data[feature]) + 1 feature_max_idx[feature] = data[feature].max() + 1 user_profile = data[["user_id", "gender", "age", "occupation", "zip"]].drop_duplicates('user_id') item_profile = data[["movie_id"]].drop_duplicates('movie_id') user_profile.set_index("user_id", inplace=True) user_item_list = data.groupby("user_id")['movie_id'].apply(list) train_set, test_set = gen_data_set(data, 0) train_model_input, train_label = gen_model_input(train_set, user_profile, SEQ_LEN) test_model_input, test_label = gen_model_input(test_set, user_profile, SEQ_LEN) # 2.count #unique features for each sparse field and generate feature config for sequence feature embedding_dim = 16 user_feature_columns = [SparseFeat('user_id', feature_max_idx['user_id'], embedding_dim), SparseFeat("gender", feature_max_idx['gender'], embedding_dim), SparseFeat("age", feature_max_idx['age'], embedding_dim), SparseFeat("occupation", feature_max_idx['occupation'], embedding_dim), SparseFeat("zip", feature_max_idx['zip'], embedding_dim), VarLenSparseFeat(SparseFeat('hist_movie_id', feature_max_idx['movie_id'], embedding_dim, embedding_name="movie_id"), SEQ_LEN, 'mean', 'hist_len'), ] item_feature_columns = [SparseFeat('movie_id', feature_max_idx['movie_id'], embedding_dim)] # 3.Define Model and train K.set_learning_phase(True) import tensorflow as tf if tf.__version__ >= '2.0.0': tf.compat.v1.disable_eager_execution() model = YoutubeDNN(user_feature_columns, item_feature_columns, num_sampled=5, user_dnn_hidden_units=(64, embedding_dim)) model.compile(optimizer="adam", loss=sampledsoftmaxloss) # "binary_crossentropy") history = model.fit(train_model_input, train_label, # train_label, batch_size=256, epochs=50, verbose=1, validation_split=0.0, ) # 4. Generate user features for testing and full item features for retrieval test_user_model_input = test_model_input all_item_model_input = {"movie_id": item_profile['movie_id'].values} user_embedding_model = Model(inputs=model.user_input, outputs=model.user_embedding) item_embedding_model = Model(inputs=model.item_input, outputs=model.item_embedding) user_embs = user_embedding_model.predict(test_user_model_input, batch_size=2 ** 12) # user_embs = user_embs[:, i, :] # i in [0,k_max) if MIND item_embs = item_embedding_model.predict(all_item_model_input, batch_size=2 ** 12) # print(user_embs) # print(item_embs) # 5. [Optional] ANN search by faiss and evaluate the result test_true_label = {line[0]: [line[2]] for line in test_set} index = faiss.IndexFlatIP(embedding_dim) # faiss.normalize_L2(item_embs) index.add(item_embs) # faiss.normalize_L2(user_embs) D, I = index.search(np.ascontiguousarray(user_embs), 10) recommed_dict = {} for i, uid in enumerate(test_user_model_input['user_id']): recommed_dict.setdefault(uid, []) try: pred = [item_profile['movie_id'].values[x] for x in I[i]] recommed_dict[uid] = pred except: print(i) test_user_items = dict() for ts in test_set: if ts[0] not in test_user_items: test_user_items[ts[0]] = set(ts[1]) item_popularity = dict() for ts in train_set: for item in ts[1]: if item in item_popularity: item_popularity[item] += 1 else: item_popularity.setdefault(item, 1) precision = metric.precision(recommed_dict, test_user_items) recall = metric.recall(recommed_dict, test_user_items) coverage = metric.coverage(recommed_dict, item_set) popularity = metric.popularity(item_popularity, recommed_dict) print("precision:{:.4f}, recall:{:.4f}, coverage:{:.4f}, popularity:{:.4f}".format(precision, recall, coverage, popularity))
def test_frcnn(options): config_output_filename = options.config_filename with open(config_output_filename, 'rb') as f_in: C = pickle.load(f_in) if C.network == 'resnet50': from utils import rpn_res as rpn from utils import classifier_res as classifier_func from utils import nn_base_res as nn_base elif C.network == 'vgg': from utils import rpn_vgg as rpn from utils import classifier_vgg as classifier_func from utils import nn_base_vgg as nn_base # turn off any data augmentation at test time C.use_horizontal_flips = False C.use_vertical_flips = False C.rot_90 = False img_path = options.path class_mapping = C.class_mapping if 'bg' not in class_mapping: class_mapping['bg'] = len(class_mapping) class_mapping = {v: k for k, v in class_mapping.items()} print(class_mapping) class_to_color = { class_mapping[v]: np.random.randint(0, 255, 3) for v in class_mapping } C.num_rois = int(options.num_rois) if C.network == 'resnet50': num_features = 1024 elif C.network == 'vgg': num_features = 512 if K.backend() == 'theano': input_shape_img = (3, None, None) input_shape_features = (num_features, None, None) else: input_shape_img = (None, None, 3) input_shape_features = (None, None, num_features) img_input = Input(shape=input_shape_img) roi_input = Input(shape=(C.num_rois, 4)) feature_map_input = Input(shape=input_shape_features) # define the base network (resnet here, can be VGG, Inception, etc) shared_layers = nn_base(img_input, trainable=True) # define the RPN, built on the base layers num_anchors = len(C.anchor_box_scales) * len(C.anchor_box_ratios) rpn_layers = rpn(shared_layers, num_anchors) classifier = classifier_func(feature_map_input, roi_input, C.num_rois, nb_classes=len(class_mapping), trainable=True) model_rpn = Model(img_input, rpn_layers) model_classifier = Model([feature_map_input, roi_input], classifier) model_rpn, model_classifier = load_weights_frcnn(model_rpn, model_classifier, C) model_rpn.compile(optimizer='sgd', loss='mse') model_classifier.compile(optimizer='sgd', loss='mse') bbox_threshold = 0.8 if not os.path.exists("results_imgs"): os.mkdir("results_imgs") for idx, img_name in enumerate(sorted(os.listdir(img_path))): if not img_name.lower().endswith( ('.bmp', '.jpeg', '.jpg', '.png', '.tif', '.tiff')): continue print(img_name) st = time.time() filepath = os.path.join(img_path, img_name) img = cv2.imread(filepath) X, ratio = format_img(img, C) if K.backend() == 'tensorflow': X = np.transpose(X, (0, 2, 3, 1)) # get the feature maps and output from the RPN [Y1, Y2, F] = model_rpn.predict(X) R = rpn_to_roi(Y1, Y2, C, K.backend(), overlap_thresh=0.7) # convert from (x1,y1,x2,y2) to (x,y,w,h) R[:, 2] -= R[:, 0] R[:, 3] -= R[:, 1] # apply the spatial pyramid pooling to the proposed regions bboxes = {} probs = {} for jk in range(R.shape[0] // C.num_rois + 1): ROIs = np.expand_dims(R[C.num_rois * jk:C.num_rois * (jk + 1), :], axis=0) if ROIs.shape[1] == 0: break if jk == R.shape[0] // C.num_rois: # pad R curr_shape = ROIs.shape target_shape = (curr_shape[0], C.num_rois, curr_shape[2]) ROIs_padded = np.zeros(target_shape).astype(ROIs.dtype) ROIs_padded[:, :curr_shape[1], :] = ROIs ROIs_padded[0, curr_shape[1]:, :] = ROIs[0, 0, :] ROIs = ROIs_padded [P_cls, P_regr] = model_classifier.predict([F, ROIs]) for ii in range(P_cls.shape[1]): if np.max(P_cls[0, ii, :]) < bbox_threshold or np.argmax( P_cls[0, ii, :]) == (P_cls.shape[2] - 1): continue cls_name = class_mapping[np.argmax(P_cls[0, ii, :])] if cls_name not in bboxes: bboxes[cls_name] = [] probs[cls_name] = [] (x, y, w, h) = ROIs[0, ii, :] cls_num = np.argmax(P_cls[0, ii, :]) try: (tx, ty, tw, th) = P_regr[0, ii, 4 * cls_num:4 * (cls_num + 1)] tx /= C.classifier_regr_std[0] ty /= C.classifier_regr_std[1] tw /= C.classifier_regr_std[2] th /= C.classifier_regr_std[3] x, y, w, h = apply_regr(x, y, w, h, tx, ty, tw, th) except: pass bboxes[cls_name].append([ C.rpn_stride * x, C.rpn_stride * y, C.rpn_stride * (x + w), C.rpn_stride * (y + h) ]) probs[cls_name].append(np.max(P_cls[0, ii, :])) all_dets = [] for key in bboxes: bbox = np.array(bboxes[key]) new_boxes, new_probs = non_max_suppression_fast(bbox, np.array( probs[key]), overlap_thresh=0.5) for jk in range(new_boxes.shape[0]): (x1, y1, x2, y2) = new_boxes[jk, :] (real_x1, real_y1, real_x2, real_y2) = get_real_coordinates(ratio, x1, y1, x2, y2) cv2.rectangle( img, (real_x1, real_y1), (real_x2, real_y2), (int(class_to_color[key][0]), int(class_to_color[key][1]), int(class_to_color[key][2])), 2) textLabel = '{}: {}'.format(key, int(100 * new_probs[jk])) all_dets.append((key, 100 * new_probs[jk], [x1, y1, x2, y2])) (retval, baseLine) = cv2.getTextSize(textLabel, cv2.FONT_HERSHEY_COMPLEX, 1, 1) textOrg = (real_x1, real_y1 - 0) cv2.rectangle( img, (textOrg[0] - 5, textOrg[1] + baseLine - 5), (textOrg[0] + retval[0] + 5, textOrg[1] - retval[1] - 5), (0, 0, 0), 2) cv2.rectangle( img, (textOrg[0] - 5, textOrg[1] + baseLine - 5), (textOrg[0] + retval[0] + 5, textOrg[1] - retval[1] - 5), (255, 255, 255), -1) cv2.putText(img, textLabel, textOrg, cv2.FONT_HERSHEY_DUPLEX, 1, (0, 0, 0), 1) print('Elapsed time = {}'.format(time.time() - st)) print(all_dets) # cv2.imshow('img', img) # cv2.waitKey(0) cv2.imwrite('.\\results_imgs\\{}.png'.format(idx), img)
epochs=10) test_images, test_labels = next(test_batches) print(test_batches.class_indices) # cat 0. indis dog 1. indis # test_labels = test_labels[:, 0] # print(test_labels) Categories = ['Cat', 'Dog'] predictions = model.predict_generator(test_batches, steps=1) for i in predictions: print(i) #saving model model.save('dog_or_cat_mobilnet.h5') def prepare_image(file): img = image.load_img(file, target_size=(224, 224)) img_array = image.img_to_array(img) img_array_expanded_dims = np.expand_dims(img_array, axis=0) return mobilenet.preprocess_input( img_array_expanded_dims) #imagei 0 ile 1 arasında normalize eder predictions2 = model.predict(prepare_image('robin.jpg')) print(predictions2) index = int(np.argmax(predictions2)) print(Categories[index])
features = [] labels = [] # loop over all the labels in the folder count = 1 for i, label in enumerate(train_labels): cur_path = train_path + "/" + label count = 1 for image_path in glob.glob(cur_path + "/*.jpg") + glob.glob(cur_path + "/output/*.jpg"): img = image.load_img(image_path, target_size=image_size) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) feature = model.predict(x) flat = feature.flatten() features.append(flat) labels.append(label) print("processed - " + str(count)) count += 1 print("completed label - " + label) # encode the labels using LabelEncoder le = LabelEncoder() le_labels = le.fit_transform(labels) # get the shape of training labels print("training labels: {}".format(le_labels)) print("training labels shape: {}".format(le_labels.shape))
pred_model = mobile_face_net() pred_model.summary() '''Extracting the weights & transfering to the prediction model''' temp_weights_list = [] for layer in model.layers: if 'dropout' in layer.name: continue temp_layer = model.get_layer(layer.name) temp_weights = temp_layer.get_weights() temp_weights_list.append(temp_weights) for i in range(len(pred_model.layers)): pred_model.get_layer(pred_model.layers[i].name).set_weights(temp_weights_list[i]) '''Verifying the results''' import numpy as np x = np.random.rand(1, 112, 112, 3) dense1_layer_model = Model(inputs=model.input, outputs=model.get_layer('dense').output) y1 = dense1_layer_model.predict(x)[0] y2 = pred_model.predict(x)[0] for i in range(128): assert y1[i] == y2[i] '''Saving the model''' pred_model.save(r'./Models/MobileFaceNet_tfkeras.h5')
print('Train loss:', score[0]) print('Train accuracy:', score[1]) score = model.evaluate(x=X_test, y=y_test) print('Test loss:', score[0]) print('Test accuracy:', score[1]) predictions = model.predict(x=X_test) layer_name = "dense_3" intermediate_layer_model = Model(inputs=model.input, outputs=model.get_layer(layer_name).output) intermediate_output_train = intermediate_layer_model.predict(x=X_train) intermediate_output_test = intermediate_layer_model.predict(x=X_test) #get_3rd_layer_output = K.function([model.layers[0].input, K.learning_phase()],[model.layers[3].output]) # Testing # test = np.random.random(input_shape)[np.newaxis,...] # layer_outs = [func([test, 1.]) for func in functors] # print layer_outs print(intermediate_output_train.shape) print(intermediate_output_test.shape) final = [] pickle_out = open(os.path.join(DATADIR, "intermediate_output_train.pickle"),
"""Translate a single text-string.""" # Convert the input-text to integer-tokens. # Note the sequence of tokens has to be reversed. # Padding is probably not necessary. input_tokens = tokenizer_src.text_to_tokens(text=input_text, reverse=True, padding=True) # Get the output of the encoder's GRU which will be # used as the initial state in the decoder's GRU. # This could also have been the encoder's final state # but that is really only necessary if the encoder # and decoder use the LSTM instead of GRU because # the LSTM has two internal states. initial_state = model_encoder.predict(input_tokens) # Max number of tokens / words in the output sequence. max_tokens = tokenizer_dest.max_tokens # Pre-allocate the 2-dim array used as input to the decoder. # This holds just a single sequence of integer-tokens, # but the decoder-model expects a batch of sequences. shape = (1, max_tokens) decoder_input_data = np.zeros(shape=shape, dtype=np.int) # The first input-token is the special start-token for 'ssss '. token_int = token_start # Initialize an empty output-text. output_text = ''
def train(df, **kwargs): save = kwargs.pop('save', True) model_path = kwargs.pop('model_path', './') opt_label = kwargs.pop('opt_label', 'test') plot_loss = kwargs.pop('plot_loss', False) features = l2_branches # imported from tools #features = ['L1'+b for b in l1_branches] # imported from tools truth_columns = [] prediction_columns = [] for c in df.columns: if ('pass' in c): truth_columns.append(c) prediction_columns.append(c.replace('pass', 'pred')) for c in prediction_columns: df[c] = -1 if len(truth_columns)==1: return df, truth_columns[0] elif len(truth_columns)<1: return df, None nfolds = 4 for i in range(nfolds): label = f'dnn_{opt_label}_{i}'.replace(' ', '_') train_folds = [(i + f) % nfolds for f in [0, 1]] val_folds = [(i + f) % nfolds for f in [2]] eval_folds = [(i + f) % nfolds for f in [3]] print(f"Train classifier #{i + 1} out of {nfolds}") print(f"Training folds: {train_folds}") print(f"Validation folds: {val_folds}") print(f"Evaluation folds: {eval_folds}") train_filter = df.event.mod(nfolds).isin(train_folds) val_filter = df.event.mod(nfolds).isin(val_folds) eval_filter = df.event.mod(nfolds).isin(eval_folds) df_train = df[train_filter] df_val = df[val_filter] df_eval = df[eval_filter] x_train = df_train[features] y_train = df_train[truth_columns].astype(int) x_val = df_val[features] y_val = df_val[truth_columns].astype(int) x_eval = df_eval[features] y_eval = df_eval[truth_columns].astype(int) input_dim = len(features) output_dim = len(truth_columns) inputs = Input(shape=(input_dim,), name=label+'_input') x = Dense(128, name=label+'_layer_1', activation='tanh')(inputs) x = Dropout(0.2)(x) x = BatchNormalization()(x) x = Dense(64, name=label+'_layer_2', activation='tanh')(x) x = Dropout(0.2)(x) x = BatchNormalization()(x) x = Dense(32, name=label+'_layer_3', activation='tanh')(x) x = Dropout(0.2)(x) x = BatchNormalization()(x) outputs = Dense(output_dim, name=label+'_output', activation='sigmoid')(x) dnn = Model(inputs=inputs, outputs=outputs) dnn.compile( loss='binary_crossentropy', optimizer='adam', metrics=["accuracy"]) dnn.summary() history = dnn.fit( x_train[features], y_train, epochs=100, batch_size=256, verbose=0, validation_data=(x_val[features], y_val), shuffle=True ) if save: save_path = f"{model_path}/{label}.pb" print(f'Saving model to {save_path}') tensorflow.compat.v1.add_to_collection('features', features) cmsml.tensorflow.save_graph(save_path, dnn, variables_to_constants=True) cmsml.tensorflow.save_graph(save_path+'.txt', dnn, variables_to_constants=True) if plot_loss: plot_loss(history, label, OUT_PATH) prediction = pd.DataFrame(dnn.predict(x_eval)) df.loc[eval_filter, prediction_columns] = prediction.values print(prediction_columns) df['best_guess_label'] = df[prediction_columns].idxmax(axis=1) df['best_guess_label'] = df.best_guess_label.str.replace('pred: ', 'pass: '******'DNN optimization: {opt_label}' df[pred_label] = df.lookup(df.index, df.best_guess_label) return df, pred_label
log_folder = 'resnet-log' NUM_EPOCHS = 6 #STEP_SIZE_TRAIN=train_generator.n//train_generator.batch_size cb_checkpointer = ModelCheckpoint(filepath=log_folder + '/best.hdf5', monitor='val_binary_accuracy', save_best_only=True, mode='auto') # csv_record_train = CSVLogger(log_folder + '/training.log') history = model.fit(train_flow, epochs=NUM_EPOCHS, validation_data=test_flow, callbacks=[cb_checkpointer, csv_record_train]) ################# PREDICTIONS ################# val_summary = model.evaluate(test_flow) preds = model.predict(test_flow) ############## CSV ############## def export_csv(pred, filenames, log_folder, metrics_names, val_summary): pred_class = np.where(pred < 0.5, 0, 1) filenames = np.asarray(filenames) filenames = filenames.reshape((len(filenames), 1)) conc = np.concatenate((filenames, pred, pred_class), axis=1) with open(log_folder + '/pred.csv', 'a') as csvfile: csvfile.write("\nMETRICS\n%s:%s " % (metrics_names[0], val_summary[0])) csvfile.write("%s:%s\n" % (metrics_names[1], val_summary[1])) csvfile.write("FILENAMES\n") np.savetxt(csvfile, conc, delimiter=",", fmt='%s')
# Encoder encoder = Model( input_img, encoded ) # Decoder encoded_input = Input( shape=(encoding_dim,) ) decoder_layer = autoencoder.layers[-1] decoder = Model( encoded_input, decoder_layer(encoded_input) ) autoencoder.compile( optimizer = 'adadelta', loss = 'binary_crossentropy' ) autoencoder.fit( x_train, x_train, epochs = 50, batch_size = 256, shuffle = True, validation_data = (x_test,x_test) ) encoded_imgs = encoder.predict( x_test ) decoded_imgs = decoder.predict( encoded_imgs ) # Save model model_name = logs_path + "/ae" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") with open( model_name+".yaml", "w" ) as model_yaml: model_yaml.write( autoencoder.to_yaml() ) autoencoder.save_weights( model_name+".h5" ) print "Model saved as '" + model_name + "'" # n = 10 # plt.figure( figsize=(20,4) ) # for i in range(n): # ax = plt.subplot( 2, n, i+1 )
class JointEmbeddingModel: def __init__(self, config): self.data_dir = config.data_dir self.model_name = config.model_name self.meth_name_len = config.methname_len # the max length of method name self.apiseq_len = config.apiseq_len self.tokens_len = config.tokens_len self.desc_len = config.desc_len self.vocab_size = config.n_words # the size of vocab self.embed_dims = config.embed_dims self.lstm_dims = config.lstm_dims self.hidden_dims = config.hidden_dims self.margin = 0.05 self.init_embed_weights_meth_name = config.init_embed_weights_methodname self.init_embed_weights_tokens = config.init_embed_weights_tokens self.init_embed_weights_desc = config.init_embed_weights_desc self.meth_name = Input(shape=(self.meth_name_len,), dtype='int32', name='meth_name') self.apiseq = Input(shape=(self.apiseq_len,), dtype='int32', name='apiseq') self.tokens = Input(shape=(self.tokens_len,), dtype='int32', name='tokens2') self.desc_good = Input(shape=(self.desc_len,), dtype='int32', name='desc_good') self.desc_bad = Input(shape=(self.desc_len,), dtype='int32', name='desc_bad') if not os.path.exists(self.data_dir + 'model/' + self.model_name): os.makedirs(self.data_dir + 'model/' + self.model_name) def build(self): self.transformer_meth = transformer.EncoderModel(vocab_size=self.vocab_size, model_dim=self.hidden_dims, embed_dim=self.embed_dims, ffn_dim=self.lstm_dims, droput_rate=0.2, n_heads=2, max_len=self.meth_name_len, name='methT') self.transformer_apiseq = transformer.EncoderModel(vocab_size=self.vocab_size, model_dim=self.hidden_dims, embed_dim=self.embed_dims, ffn_dim=self.lstm_dims, droput_rate=0.2, n_heads=4, max_len=self.apiseq_len, name='apiseqT') self.transformer_desc = transformer.EncoderModel(vocab_size=self.vocab_size, model_dim=self.hidden_dims, embed_dim=self.embed_dims, ffn_dim=self.lstm_dims, droput_rate=0.2, n_heads=4, max_len=self.desc_len, name='descT') # self.transformer_ast = EncoderModel(vocab_size=self.vocab_size, model_dim=self.hidden_dims, embed_dim=self.embed_dims, ffn_dim=self.lstm_dims, droput_rate=0.2, n_heads=4, max_len=128) self.transformer_tokens = transformer.EncoderModel(vocab_size=self.vocab_size, model_dim=self.hidden_dims, embed_dim=self.embed_dims, ffn_dim=self.lstm_dims, droput_rate=0.2, n_heads=8, max_len=self.tokens_len, name='tokensT') # create path to store model Info # 1 -- CodeNN meth_name = Input(shape=(self.meth_name_len,), dtype='int32', name='meth_name') apiseq = Input(shape=(self.apiseq_len,), dtype='int32', name='apiseq') tokens3 = Input(shape=(self.tokens_len,), dtype='int32', name='tokens3') # method name # embedding layer meth_name_out = self.transformer_meth(meth_name) # max pooling maxpool = Lambda(lambda x: k.max(x, axis=1, keepdims=False), output_shape=lambda x: (x[0], x[2]), name='maxpooling_methodname') method_name_pool = maxpool(meth_name_out) activation = Activation('tanh', name='active_method_name') method_name_repr = activation(method_name_pool) # apiseq # embedding layer apiseq_out = self.transformer_apiseq(apiseq) # max pooling maxpool = Lambda(lambda x: k.max(x, axis=1, keepdims=False), output_shape=lambda x: (x[0], x[2]), name='maxpooling_apiseq') apiseq_pool = maxpool(apiseq_out) activation = Activation('tanh', name='active_apiseq') apiseq_repr = activation(apiseq_pool) # tokens # embedding layer tokens_out = self.transformer_tokens(tokens3) # max pooling maxpool = Lambda(lambda x: k.max(x, axis=1, keepdims=False), output_shape=lambda x: (x[0], x[2]), name='maxpooling_tokens') tokens_pool = maxpool(tokens_out) activation = Activation('tanh', name='active_tokens') tokens_repr = activation(tokens_pool) # fusion method_name, apiseq, tokens merge_method_name_api = Concatenate(name='merge_methname_api')([method_name_repr, apiseq_repr]) merge_code_repr = Concatenate(name='merge_code_repr')([merge_method_name_api, tokens_repr]) code_repr = Dense(self.hidden_dims, activation='tanh', name='dense_coderepr')(merge_code_repr) self.code_repr_model = Model(inputs=[meth_name, apiseq, tokens3], outputs=[code_repr], name='code_repr_model') self.code_repr_model.summary() self.output = Model(inputs=self.code_repr_model.input, outputs=self.code_repr_model.get_layer('tokensT').output) self.output.summary() # 2 -- description desc = Input(shape=(self.desc_len,), dtype='int32', name='desc') # desc # embedding layer desc_out = self.transformer_desc(desc) # max pooling maxpool = Lambda(lambda x: k.max(x, axis=1, keepdims=False), output_shape=lambda x: (x[0], x[2]), name='maxpooling_desc') desc_pool = maxpool(desc_out) activation = Activation('tanh', name='active_desc') desc_repr = activation(desc_pool) self.desc_repr_model = Model(inputs=[desc], outputs=[desc_repr], name='desc_repr_model') self.desc_repr_model.summary() # 3 -- cosine similarity code_repr = self.code_repr_model([meth_name, apiseq, tokens3]) desc_repr = self.desc_repr_model([desc]) cos_sim = Dot(axes=1, normalize=True, name='cos_sim')([code_repr, desc_repr]) sim_model = Model(inputs=[meth_name, apiseq, tokens3, desc], outputs=[cos_sim], name='sim_model') self.sim_model = sim_model self.sim_model.summary() # 4 -- build training model good_sim = sim_model([self.meth_name, self.apiseq, self.tokens, self.desc_good]) bad_sim = sim_model([self.meth_name, self.apiseq, self.tokens, self.desc_bad]) loss = Lambda(lambda x: k.maximum(1e-6, self.margin - (x[0] - x[1])), output_shape=lambda x: x[0], name='loss')( [good_sim, bad_sim]) self.training_model = Model(inputs=[self.meth_name, self.apiseq, self.tokens, self.desc_good, self.desc_bad], outputs=[loss], name='training_model') self.training_model.summary() def compile(self, optimizer, **kwargs): optimizer = keras.optimizers.SGD(lr=0.0001, momentum=0.9, nesterov=True) # optimizer = keras.optimizers.Adam(lr=0.0001) # print(self.code_repr_model.layers, self.desc_repr_model.layers, self.training_model.layers, self.sim_model.layers) self.code_repr_model.compile(loss='cosine_proximity', optimizer=optimizer, **kwargs) self.desc_repr_model.compile(loss='cosine_proximity', optimizer=optimizer, **kwargs) self.training_model.compile(loss=lambda y_true, y_pred: y_pred + y_true - y_true, optimizer=optimizer, **kwargs) self.sim_model.compile(loss='binary_crossentropy', optimizer=optimizer, **kwargs) def fit(self, x, **kwargs): y = np.zeros(shape=x[0].shape[:1], dtype=np.float32) return self.training_model.fit(x, y, **kwargs) def getOutput(self, x): # functor = k.function([self.code_repr_model.layers[0].input, k.learning_phase()], [self.code_repr_model.layers[0].output]) # print(functor(x)[0]) print(self.output.predict(x)) def repr_code(self, x, **kwargs): return self.code_repr_model.predict(x, **kwargs) def repr_desc(self, x, **kwargs): return self.desc_repr_model.predict(x, **kwargs) def predict(self, x, **kwargs): return self.sim_model.predict(x, **kwargs) def save(self, code_model_file, desc_model_file, **kwargs): file = h5py.File(code_model_file, 'w') weight_code = self.code_repr_model.get_weights() for i in range(len(weight_code)): file.create_dataset('weight_code'+str(i), data=weight_code[i]) file.close() file = h5py.File(desc_model_file, 'w') weight_desc = self.desc_repr_model.get_weights() for i in range(len(weight_desc)): file.create_dataset('weight_desc'+str(i), data=weight_desc[i]) file.close() # self.code_repr_model.save_weights(code_model_file, **kwargs) # self.desc_repr_model.save_weights(desc_model_file, **kwargs) def load(self, code_model_file, desc_model_file, **kwargs): # self.code_repr_model.load_weights(code_model_file, **kwargs) # self.desc_repr_model.load_weights(desc_model_file, **kwargs) file = h5py.File(code_model_file, 'r') weight_code = [] for i in range(len(file.keys())): weight_code.append(file['weight_code'+str(i)][:]) self.code_repr_model.set_weights(weight_code) file.close() file = h5py.File(desc_model_file, 'r') weight_desc = [] for i in range(len(file.keys())): weight_desc.append(file['weight_desc'+str(i)][:]) self.desc_repr_model.set_weights(weight_desc) file.close()
def show_text_tag_features(self, train_data, show_num=10): """ 检查生成的mashup和api的text和tag的特征是否正常 """ if self.old_new == 'old': m_ids, a_ids = train_data[:-1] instances_tuple = self.get_instances(m_ids[:show_num], a_ids[:show_num]) elif self.old_new == 'new': m_ids, a_ids, slt_a_ids = train_data[:-1] instances_tuple = self.get_instances(m_ids[:show_num], a_ids[:show_num], slt_a_ids[:show_num]) text_tag_middle_model = Model( inputs=[*self.model.inputs], outputs=[ *self.model.get_layer('all_content_concatenate').input[:4] ]) mashup_text_features, apis_text_features, mashup_tag_features, apis_tag_features = text_tag_middle_model.predict( [*instances_tuple], verbose=0) mashup_text_features_path = os.path.join(self.model_dir, 'mashup_text_features.dat') apis_text_features_path = os.path.join(self.model_dir, 'apis_text_features.dat') mashup_tag_features_path = os.path.join(self.model_dir, 'mashup_tag_features.dat') apis_tag_features_path = os.path.join(self.model_dir, 'apis_tag_features.dat') save_2D_list(mashup_text_features_path, mashup_text_features, 'a+') save_2D_list(apis_text_features_path, apis_text_features, 'a+') save_2D_list(mashup_tag_features_path, mashup_tag_features, 'a+') save_2D_list(apis_tag_features_path, apis_tag_features, 'a+')
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Project : tql-Python. # @File : mid_layer # @Time : 2019-07-12 16:33 # @Author : yuanjie # @Email : [email protected] # @Software : PyCharm # @Description : """ https://www.tensorflow.org/beta/tutorials/keras/feature_columns """ from tensorflow.python.keras.layers import Input, Embedding, Reshape, Activation from tensorflow.python.keras.models import Model input_model = Input(shape=(1, )) output_store = Embedding(1115, 10, name='store_embedding')(input_model) output_store = Reshape(target_shape=(10, ))(output_store) output_model = Activation('sigmoid')(output_store) model = Model(inputs=input_model, outputs=output_model) model.summary() embed = Model(inputs=model.input, outputs=model.get_layer(index=1).output) # 以这个model的预测值作为输出 embed.predict([[1]])
# In[ ]: def read(): tmp = [] for i in range(10500): wav = process_wav_file('data/test/audio/' + str(i + 1) + '.wav') tmp.append(wav) return np.array(tmp) # In[ ]: predictions = model.predict(read()) # In[ ]: classes = np.argmax(predictions, axis=1) # In[ ]: # last batch will contain padding, so remove duplicates submission = dict() for i in range(len(test_paths)): fname, label = os.path.basename(test_paths[i]), id2name[classes[i]] submission[fname] = label # In[ ]:
def nfold_train(train_data, train_label, model_types=None, stacking=False, valide_data=None, valide_label=None, test_data=None, train_weight=None, valide_weight=None, flags=None, tokenizer=None, scores=None, emb_weight=None, cat_max=None, leak_target=None): """ nfold Training """ print("Over all training size:") print(train_data.shape) print("Over all label size:") print(train_label.shape) fold = flags.nfold kf = KFold(n_splits=fold, shuffle=True, random_state=100) # wv_model = gensim.models.Word2Vec.load("wv_model_norm.gensim") stacking = flags.stacking stacking_data = pd.Series([np.zeros(1)] * train_data.shape[0]) stacking_label = pd.Series([np.zeros(1)] * train_data.shape[0]) test_preds = None num_fold = 0 models = [] losses = [] train_part_img_id = [] validate_part_img_id = [] for train_index, test_index in kf.split(train_data): # print(test_index[:100]) # exit(0) if valide_label is None: train_img = preprocess_img(train_data['img']) train_part = train_img[train_index] train_part_label = train_label[train_index] validate_part = train_img[test_index] validate_part_label = train_label[test_index] train_part_img_id.append(train_data.iloc[train_index].img_id) validate_part_img_id.append(train_data.iloc[test_index].img_id) print('\nfold: %d th train :-)' % (num_fold)) print('Train size: {} Valide size: {}'.format( train_part_label.shape[0], validate_part_label.shape[0])) print('Train target nunique: ', np.unique(np.argwhere(train_part_label == 1)[:, 1]).shape[0], 'Validate target nuique: ', np.unique(np.argwhere(validate_part_label == 1)[:, 1]).shape[0]) onefold_models = [] for model_type in model_types: if model_type == 'k' or model_type == 'r': # with tf.device('/cpu:0'): model = DNN_Model(scores=scores, cat_max=train_part_label.shape[1], flags=flags, emb_weight=emb_weight, model_type=model_type) if num_fold == 0: print(model.model.summary()) model.train(train_part, train_part_label, validate_part, validate_part_label) onefold_models.append((model, model_type)) if stacking: flat_model = Model( inputs=model.model.inputs, outputs=model.model.get_layer(name='avg_pool').output) stacking_data[test_index] = list( flat_model.predict(validate_part)) stacking_label[test_index] = list(model.predict(validate_part)) models.append(onefold_models[0]) num_fold += 1 if num_fold == flags.ensemble_nfold: break # if stacking: # test_preds /= flags.ensemble_nfold # test_data = np.c_[test_data, test_preds] return models, stacking_data, stacking_label, test_preds, train_part_img_id, validate_part_img_id
if tta == 1: x_batch = [] x_batch_1d = [] x2d, x1d = process_wav_file(path, phase='TEST', dim='combi') x_batch.append(x2d) x_batch_1d.append(x1d) x_batch = np.array(x_batch) x_batch_1d = np.array(x_batch_1d) return [x_batch, x_batch_1d] subfile = open(root_dir + weight_name + '_sub' + '.csv', 'w') probfile = open(root_dir + weight_name + '_prob' + '.csv', 'w') subfile.write('fname,label\n') probfile.write( 'fname,yes,no,up,down,left,right,on,off,stop,go,silence,unknown\n') for idx, path in enumerate(test_paths): fname = path.split('\\')[-1] probs = model.predict(get_test_set_1d(path), batch_size=1) label = id2name[np.argmax(probs)] subfile.write('{},{}\n'.format(fname, label)) probfile.write(fname + ',') print(str(idx) + '/' + str(len(test_paths))) for p, prob in enumerate(probs[0]): probfile.write(str(prob)) if p == 11: probfile.write('\n') else: probfile.write(',')
class KimCNN: def __init__(self, embedding_map, tokenizer, model_config): self.model_config = model_config self.tokenizer = tokenizer self.embedding_map = embedding_map self.model = None def create_model(self): # Declaration for KimCNN-based encoder encoder_input = Input(shape=(self.model_config.max_sequence_len, ), dtype='int32') embedding_layer = Embedding( len(self.tokenizer.word_index) + 1, self.model_config.word_embedding_dim, weights=[self.embedding_map], input_length=self.model_config.max_sequence_len, trainable=False) embedded_sequences = embedding_layer(encoder_input) l_conv1 = Conv1D(100, 3, activation='relu', padding='same')(embedded_sequences) l_pool1 = GlobalMaxPool1D()(l_conv1) l_conv2 = Conv1D(100, 4, activation='relu', padding='same')(embedded_sequences) l_pool2 = GlobalMaxPool1D()(l_conv2) l_conv3 = Conv1D(100, 5, activation='relu', padding='same')(embedded_sequences) l_pool3 = GlobalMaxPool1D()(l_conv3) l_concat1 = Concatenate()([l_pool1, l_pool2, l_pool3]) encoder = Model(encoder_input, l_concat1) # Similarity classifier using the KimCNN-based encoder sequence_input1 = Input(shape=(self.model_config.max_sequence_len, ), dtype='int32') sequence_input2 = Input(shape=(self.model_config.max_sequence_len, ), dtype='int32') l_concat2 = Concatenate()( [encoder(sequence_input1), encoder(sequence_input2)]) l_dense1 = Dense(self.model_config.hidden_dim, activation='relu')(l_concat2) l_dropout1 = Dropout(self.model_config.dropout)(l_dense1) preds = Dense(self.model_config.num_classes, activation='softmax')(l_dropout1) self.model = Model([sequence_input1, sequence_input2], preds) self.model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) self.model.summary() def train(self, train_x1, train_x2, train_y, evaluate_x1, evaluate_x2, evaluate_y, **kwargs): iteration = 0 if 'iteration' in kwargs: iteration = kwargs['iteration'] early_stopping_callback = EarlyStopping( patience=self.model_config.patience, monitor='val_acc') checkpoint_callback = ModelCheckpoint( filepath="data/checkpoints/kim_cnn/%s_%d.hdf5" % (self.model_config.dataset, iteration), monitor='val_acc', verbose=1, save_best_only=True) self.model.fit( [train_x1, train_x2], train_y, validation_data=([evaluate_x1, evaluate_x2], evaluate_y), epochs=self.model_config.epochs, batch_size=self.model_config.batch_size, callbacks=[early_stopping_callback, checkpoint_callback]) self.model.load_weights( filepath="data/checkpoints/kim_cnn/%s_%d.hdf5" % (self.model_config.dataset, iteration)) def predict(self, predict_x1, predict_x2): return self.model.predict([predict_x1, predict_x2]) def evaluate(self, evaluate_x1, evaluate_x2, evaluate_y): predict_y = self.predict(evaluate_x1, evaluate_x2).argmax(axis=1) evaluate_y = evaluate_y.argmax(axis=1) return { "individual": precision_recall_fscore_support(evaluate_y, predict_y), "micro-average": precision_recall_fscore_support(evaluate_y, predict_y, average="micro") } def cross_val(self, data_x1, data_x2, data_y, n_splits=5): skf = StratifiedKFold(n_splits, shuffle=False, random_state=157) print("Performing cross validation (%d-fold)..." % n_splits) iteration = 1 mean_accuracy = 0 recall_list = [0 for _ in range(self.model_config.num_classes)] precision_list = [0 for _ in range(self.model_config.num_classes)] for train_index, test_index in skf.split(data_x1, data_y.argmax(axis=1)): self.create_model() print("Iteration %d of %d" % (iteration, n_splits)) self.train(data_x1[train_index], data_x2[train_index], data_y[train_index], data_x1[test_index], data_x2[test_index], data_y[test_index], iteration=iteration) metrics = self.evaluate(data_x1[test_index], data_x2[test_index], data_y[test_index]) precision_list = [ x + y for x, y in zip(metrics['individual'][0], precision_list) ] recall_list = [ x + y for x, y in zip(metrics['individual'][1], recall_list) ] mean_accuracy += metrics['micro-average'][0] print("Precision, Recall, F_Score, Support") iteration += 1 print(metrics) print("Mean accuracy: %s Mean precision: %s, Mean recall: %s" % (mean_accuracy / n_splits, [ precision / n_splits for precision in precision_list ], [recall / n_splits for recall in recall_list]))
feature_less=FEATURE_LESS, ) model.compile(optimizer=Adam(0.01), loss='categorical_crossentropy', weighted_metrics=['categorical_crossentropy', 'acc']) NB_EPOCH = 200 PATIENCE = 200 # early stopping patience val_data = (model_input, y_val, val_mask) mc_callback = ModelCheckpoint('./best_model.h5', monitor='val_weighted_categorical_crossentropy', save_best_only=True, save_weights_only=True) # train print("start training") model.fit(model_input, y_train, sample_weight=train_mask, validation_data=val_data, batch_size=A.shape[0], epochs=NB_EPOCH, shuffle=False, verbose=2, callbacks=[mc_callback]) # test model.load_weights('./best_model.h5') eval_results = model.evaluate( model_input, y_test, sample_weight=test_mask, batch_size=A.shape[0]) print('Done.\n' 'Test loss: {}\n' 'Test weighted_loss: {}\n' 'Test accuracy: {}'.format(*eval_results)) embedding_model = Model(model.input, outputs=Lambda(lambda x: model.layers[-1].output)(model.input)) embedding_weights = embedding_model.predict(model_input, batch_size=A.shape[0]) y = np.genfromtxt("{}{}.content".format('../data/cora/', 'cora'), dtype=np.dtype(str))[:, -1] plot_embeddings(embedding_weights, np.arange(A.shape[0]), y)
class ConvMnist: def __init__(self, filename=None): ''' 学習済みモデルファイルをロードする (optional) ''' self.model = None if filename is not None: print('load model: ', filename) self.model = load_model(filename) self.model.summary() def train(self): ''' 学習する ''' # MNISTの学習用データ、テストデータをロードする (x_train, y_train), (x_test, y_test) = mnist.load_data() # 学習データの前処理 # X: 6000x28x28x1のTensorに変換し、値を0~1.0に正規化 # Y: one-hot化(6000x1 -> 6000x10) x_train = x_train.reshape(x_train.shape[0], x_train.shape[1], x_train.shape[2], 1) x_test = x_test.reshape(x_test.shape[0], x_test.shape[1], x_test.shape[2], 1) x_train = x_train / 255. x_test = x_test / 255. y_train = to_categorical(y_train, 10) y_test = to_categorical(y_test, 10) # 学習状態は悪用のTensorBoard設定 tsb = TensorBoard(log_dir='./logs') # Convolutionモデルの作成 input = Input(shape=(28,28,1)) conv1 = Conv2D( filters=8, kernel_size=(3,3), strides=(1,1), padding='same', activation='relu' )(input) pool1 = MaxPooling2D(pool_size=(2,2))(conv1) conv2 = Conv2D( filters=4, kernel_size=(3,3), strides=(1,1), padding='same', activation='relu' )(pool1) dropout1 = Dropout(0.2)(conv2) flatten1 = Flatten()(dropout1) output = Dense(units=10, activation='softmax')(flatten1) self.model = Model(inputs=[input], outputs=[output]) self.model.summary() self.model.compile( optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'] ) # Convolutionモデルの学習 self.model.fit( x_train, y_train, batch_size=128, epochs=10, validation_split=0.2, # callbacks=[tsb], ) # 学習したモデルを使用して、テスト用データで評価する score = self.model.evaluate(x_test, y_test, verbose=0) print("test data score: ", score) def save_trained_model(self, filename): ''' 学習済みモデルをファイル(h5)に保存する ''' self.model.save(filename) def predict(self, input_image): ''' 1つのグレースケール入力画像(28x28のndarray)に対して、数字(0~9)を判定する ret: result, score ''' if input_image.shape != (28,28): return -1, -1 input_image = input_image.reshape(1, input_image.shape[0], input_image.shape[1], 1) input_image = input_image / 255. probs = self.model.predict(input_image) result = np.argmax(probs[0]) return result, probs[0][result]
for (index, replacement) in zip(unique, range(len(unique))): ID_list[ID_list == index] = replacement return imgs, ID_list # #dataArrays test_image_array, test_ID_list = parse(test_dir) train_image_array, train_ID_list = parse(train_dir) print("arrays created") # #get output from VGG layer_name = 'fc2' fc2_layer_model = Model(inputs=model.input, outputs=model.get_layer(layer_name).output) #get train and test features feature = fc2_layer_model.predict(train_image_array) testfeature = fc2_layer_model.predict(test_image_array) print("features created") #SVM fitting scaler = StandardScaler() scaler.fit(feature) feature = scaler.transform(feature) testfeature = scaler.transform(testfeature) svc = LinearSVC(random_state=5) svc.fit(testfeature, test_ID_list) ycap = svc.predict(testfeature) print(cfm(test_ID_list, ycap)) print('test score is :', svc.score(testfeature, test_ID_list))
class PPOPolicyBrain: def __init__( self, state_dim: int, output_dim: int, learning_rate: float = 0.0001, hidden_layers_count: int = 0, neurons_per_hidden_layer: int = 0, ): state_tensor = Input((state_dim,), name="state") mask_tensor = Input((output_dim,), name="mask") advantages_tensor = Input((1,), name="advantages") old_policy_tensor = Input((output_dim,), name="old_policy") hidden_tensor = state_tensor for i in range(hidden_layers_count): hidden_tensor = Dense(neurons_per_hidden_layer, activation=tanh)( hidden_tensor ) hidden_tensor = Dense(output_dim, activation=linear)(hidden_tensor) policy_tensor = Lambda(lambda t: softmax_with_mask(t))( (hidden_tensor, mask_tensor) ) self.model = Model( [state_tensor, mask_tensor, advantages_tensor, old_policy_tensor], [policy_tensor], ) # print(self.model.summary()) loss = build_ppo_loss(advantages_tensor, old_policy_tensor) self.model.compile(loss=loss, optimizer=Adam(lr=learning_rate)) def predict(self, state: np.ndarray, mask: np.ndarray) -> np.ndarray: return self.model.predict( ( np.array((state,)), np.array((mask,)), np.zeros((1, 1)), np.ones((1, mask.shape[0])), ) )[0] def train( self, states: np.ndarray, masks: np.ndarray, chosen_action_masks: np.ndarray, advantages: np.ndarray, ): old_predictions = self.model.predict( (states, masks, np.zeros((states.shape[0], 1)), np.ones_like(masks),) ) self.model.fit( (states, masks, advantages, old_predictions), (chosen_action_masks,), epochs=10, batch_size=64, verbose=0, ) def save_model(self, filename: str): self.model.save(f"{filename}_actor.h5") def load_model(self, filename: str): self.model = load_model(filename)
units=embedding_dim, num_sampled=100, ) model.compile(optimizer='adam', loss=sampledsoftmaxloss) # "binary_crossentropy") history = model.fit(train_model_input, train_label, # train_label, batch_size=512, epochs=1, verbose=1, validation_split=0.0, ) K.set_learning_phase(False) # 3.Define Model,train,predict and evaluate test_user_model_input = test_model_input all_item_model_input = {"movie_id": item_profile['movie_id'].values, } user_embedding_model = Model(inputs=model.user_input, outputs=model.user_embedding) item_embedding_model = Model(inputs=model.item_input, outputs=model.item_embedding) user_embs = user_embedding_model.predict(test_user_model_input, batch_size=2 ** 12) # user_embs = user_embs[:, i, :] # i in [0,k_max) if MIND item_embs = item_embedding_model.predict(all_item_model_input, batch_size=2 ** 12) print(user_embs.shape) print(item_embs.shape) # test_true_label = {line[0]: [line[3]] for line in test_set} # # import numpy as np # import faiss # from tqdm import tqdm # from deepmatch.utils import recall_N # # index = faiss.IndexFlatIP(embedding_dim) # # faiss.normalize_L2(item_embs)
model = Model(inputs=[encoder_input, decoder_input], outputs=mu) model.summary() model.compile(optimizer = 'adam',loss = gaussian_likelihood(sigma),metrics = ['mse']) es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=30,restore_best_weights = True) train_history = model.fit([X_train,X_covars_train], y_train, batch_size = batch_size, epochs = 5000, validation_data =[[X_val,X_covars_val], y_val] ,callbacks = [es]) predictor = Model(inputs = [encoder_input,decoder_input],outputs = [mu,sigma]) plt.clf() plt.plot(train_history.history['loss']) plt.plot(train_history.history['val_loss']) split = 'val' j=0 j+=1 key = list(train_data_dict.keys())[j] df_transformer = data_transformer(train_data_dict,key,pred_period,look_back_period,encoder_inputs,decoder_inputs, n_validation_intervals = n_validation_intervals) preds = predictor.predict([df_transformer['X_'+split],df_transformer['X_covars_'+split]]) sigma = preds[1] preds = preds[0] preds_df = TSU.pred_df(df_transformer['y_'+split][:,:,-1],preds[:,:,-1], index = df_transformer['period_'+split][:,:,0]) pred_true_df = pd.concat([preds_df,train_data_dict[key]['Volume']],axis = 1) pred_true_df.plot(alpha = 0.8) error_df = pd.DataFrame() error_df['f_0'] = abs(pred_true_df['f_0']-pred_true_df['Volume'])/pred_true_df['Volume'] error_df['f_1'] = abs(pred_true_df['f_1']-pred_true_df['Volume'])/pred_true_df['Volume'] error_df['f_2'] = abs(pred_true_df['f_2']-pred_true_df['Volume'])/pred_true_df['Volume']
# Model Compilation model = Model(inputs=inputs, outputs=outputs) model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) # Training model.fit(x=data.train.images, y=data.train.labels, epochs=1, batch_size=128) # Evaluation result = model.evaluate(x=data.test.images, y=data.test.labels) for name, value in zip(model.metrics_names, result): print(name, value) y_pred = model.predict(x=data.test.images) print(y_pred) cls_pred = np.argmax(y_pred, axis=1) def plot_example_errors(cls_pred): incorrect = (cls_pred != data.test.cls) images = data.test.images[incorrect] cls_pred = cls_pred[incorrect] cls_true = data.test.cls[incorrect] plot_images(images=images[0:9], cls_true=cls_true[0:9], cls_pred=cls_pred[0:9])
def layer_test(layer_cls, kwargs={}, input_shape=None, input_dtype=None, input_data=None, expected_output=None, expected_output_dtype=None, fixed_batch_size=False): # generate input data if input_data is None: if not input_shape: raise AssertionError() if not input_dtype: input_dtype = K.floatx() input_data_shape = list(input_shape) for i, e in enumerate(input_data_shape): if e is None: input_data_shape[i] = np.random.randint(1, 4) if all(isinstance(e, tuple) for e in input_data_shape): input_data = [] for e in input_data_shape: input_data.append( (10 * np.random.random(e)).astype(input_dtype)) else: input_data = (10 * np.random.random(input_data_shape)) input_data = input_data.astype(input_dtype) else: if input_shape is None: input_shape = input_data.shape if input_dtype is None: input_dtype = input_data.dtype if expected_output_dtype is None: expected_output_dtype = input_dtype # instantiation layer = layer_cls(**kwargs) # test get_weights , set_weights at layer level weights = layer.get_weights() layer.set_weights(weights) try: expected_output_shape = layer.compute_output_shape(input_shape) except Exception: expected_output_shape = layer._compute_output_shape(input_shape) # test in functional API if isinstance(input_shape, list): if fixed_batch_size: x = [Input(batch_shape=e, dtype=input_dtype) for e in input_shape] else: x = [Input(shape=e[1:], dtype=input_dtype) for e in input_shape] else: if fixed_batch_size: x = Input(batch_shape=input_shape, dtype=input_dtype) else: x = Input(shape=input_shape[1:], dtype=input_dtype) y = layer(x) if not (K.dtype(y) == expected_output_dtype): raise AssertionError() # check with the functional API model = Model(x, y) actual_output = model.predict(input_data) actual_output_shape = actual_output.shape for expected_dim, actual_dim in zip(expected_output_shape, actual_output_shape): if expected_dim is not None: if not (expected_dim == actual_dim): raise AssertionError() if expected_output is not None: assert_allclose(actual_output, expected_output, rtol=1e-3) # test serialization, weight setting at model level model_config = model.get_config() recovered_model = model.__class__.from_config(model_config) if model.weights: weights = model.get_weights() recovered_model.set_weights(weights) _output = recovered_model.predict(input_data) assert_allclose(_output, actual_output, rtol=1e-3) # test training mode (e.g. useful when the layer has a # different behavior at training and testing time). if has_arg(layer.call, 'training'): model.compile('rmsprop', 'mse') model.train_on_batch(input_data, actual_output) # test instantiation from layer config layer_config = layer.get_config() layer_config['batch_input_shape'] = input_shape layer = layer.__class__.from_config(layer_config) # for further checks in the caller function return actual_output