def get_model(): inputs = Input(shape=( TIME_STEPS, INPUT_DIM, )) rnn_out = LSTM(32, return_sequences=True)(inputs) attention_output = attention_3d_block(rnn_out) output = Dense(INPUT_DIM, activation='sigmoid', name='output')(attention_output) m = Model(inputs=[inputs], outputs=[output]) print(m.summary()) return m
def train_and_evaluate_model_on_imdb(add_attention=True): numpy.random.seed(7) # load the dataset but only keep the top n words, zero the rest top_words = 5000 (X_train, y_train), (X_test, y_test) = imdb.load_data(num_words=top_words) # truncate and pad input sequences max_review_length = 500 X_train = sequence.pad_sequences(X_train, maxlen=max_review_length) X_test = sequence.pad_sequences(X_test, maxlen=max_review_length) # create the model embedding_vector_length = 32 i = Input(shape=(max_review_length, )) x = Embedding(top_words, embedding_vector_length, input_length=max_review_length)(i) x = Dropout(0.5)(x) if add_attention: x = LSTM(100, return_sequences=True)(x) x = attention_3d_block(x) else: x = LSTM(100, return_sequences=False)(x) x = Dense(350, activation='relu')( x) # same number of parameters so fair comparison. x = Dropout(0.5)(x) x = Dense(1, activation='sigmoid')(x) model = Model(inputs=[i], outputs=[x]) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) print(model.summary()) class RecordBestTestAccuracy(Callback): def __init__(self): super().__init__() self.val_accuracies = [] self.val_losses = [] def on_epoch_end(self, epoch, logs=None): self.val_accuracies.append(logs['val_accuracy']) self.val_losses.append(logs['val_loss']) rbta = RecordBestTestAccuracy() model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=10, batch_size=64, callbacks=[rbta]) print(f"Max Test Accuracy: {100 * np.max(rbta.val_accuracies):.2f} %") print(f"Mean Test Accuracy: {100 * np.mean(rbta.val_accuracies):.2f} %")
data_test.append(graph_all[i]) X_in = Input(shape=(F, ), batch_shape=(N, F)) # Define model architecture # NOTE: We pass arguments for graph convolutional layers as a list of tensors. # This is somewhat hacky, more elegant options would require rewriting the Layer base class. # H = Dropout(0.5)(X_in) H = GraphConvolution(F_, support, activation='relu', kernel_regularizer=l2(5e-4))([X_in] + G) H = Reshape((-1, F))(H) gcn_lstm = LSTM(units=50, input_shape=[N, F], return_sequences=True)(H) gcn_lstm = attention.attention_3d_block(gcn_lstm) print('gcn_lstm') print(gcn_lstm) # ------------如果是gcn_lstm------------------------- gcn_lstm = Dense(1)(gcn_lstm) # ------------------------如果是gcn_lstm_gat--------------------------------- # A_in = Input(shape=(N,), batch_shape=(N, N)) # gcn_lstm_gat = GraphAttention(N=N, # F_=F_, # attn_heads=n_attn_heads, # attn_heads_reduction='average', # dropout_rate=dropout_rate, # activation='elu',
# F_=F_, # attn_heads=n_attn_heads, # attn_heads_reduction='average', # dropout_rate=dropout_rate, # activation='elu', # kernel_regularizer=l2(l2_reg), # attn_kernel_regularizer=l2(l2_reg))([gat_lstm, A_in]) # gat_lstm = Dense(1)(gat_lstm_gat) # ------------------------------------------------------------------------------------------------------------ # ---------------------------------GAT_LSTM---------------------------------------------------------------------------- # gat_lstm = Dense(1)(gat_lstm) # ---------------------------------GAT_LSTM_att---------------------------------- # 需要将前面的LSTM的returnsequence设为true gat_lstm = attention.attention_3d_block(gat_lstm) gat_lstm = Dense(1)(gat_lstm) # ---------------------------------------------------------------------------------------------------------- model = Model(inputs=[X_in, A_in], outputs=gat_lstm) model.compile(loss='mse', optimizer='rmsprop') #=======================拟合模型,预测================================== # print('data_train') # print(data_train) print('y_train') print(y_train) print('model') print(model.summary())
def main(): numpy.random.seed(7) # data. definition of the problem. seq_length = 20 x_train, y_train = task_add_two_numbers_after_delimiter(20_000, seq_length) x_val, y_val = task_add_two_numbers_after_delimiter(4_000, seq_length) print(x_val.shape) print(y_val.shape) #sys.exit() # just arbitrary values. it's for visual purposes. easy to see than random values. test_index_1 = 4 test_index_2 = 9 x_test, _ = task_add_two_numbers_after_delimiter(10, seq_length, 0, test_index_1, test_index_2) # x_test_mask is just a mask that, if applied to x_test, would still contain the information to solve the problem. # we expect the attention map to look like this mask. x_test_mask = np.zeros_like(x_test[..., 0]) x_test_mask[:, test_index_1:test_index_1 + 1] = 1 x_test_mask[:, test_index_2:test_index_2 + 1] = 1 # model i = Input(shape=(seq_length, 1)) # [1, seq_length, 1] x = LSTM(100, return_sequences=True)(i) # [1, seq_length, 100] x = attention_3d_block(x) # [1, 128] x = Dropout(0.2)(x) #x = Dense(128, use_bias = False, activation = 'tanh')(x) x = Dense(1, activation='linear')(x) model = Model(inputs=[i], outputs=[x]) model.compile(loss='mse', optimizer='adam') print(model.summary()) output_dir = 'task_add_two_numbers' if not os.path.exists(output_dir): os.makedirs(output_dir) max_epoch = int(sys.argv[1]) if len(sys.argv) > 1 else 10 class VisualiseAttentionMap(Callback): def on_epoch_end(self, epoch, logs=None): attention_map = get_activations( model, x_test, layer_names='attention_weight')['attention_weight'] attention_map = np.max(attention_map, axis=1) print(attention_map.shape) print(x_test_mask.shape) # top is attention map. # bottom is ground truth. plt.imshow(np.concatenate([attention_map, x_test_mask]), cmap='hot') iteration_no = str(epoch).zfill(3) plt.axis('off') plt.title(f'Iteration {iteration_no} / {max_epoch}') plt.savefig(f'{output_dir}/epoch_{iteration_no}.png') plt.close() plt.clf() model.fit(x_train, y_train, validation_data=(x_val, y_val), epochs=max_epoch, batch_size=64, callbacks=[VisualiseAttentionMap()]) #model.fit(x_train, y_train, validation_data=(x_val, y_val), epochs=max_epoch, # batch_size=64) y_predicted = model.predict(x_val[:10]) print(y_val.shape) print(y_predicted.shape) print(y_val[:10]) print(y_predicted[:10])
data_train = [] data_test = [] # print('data.shape[0]') # print(data.shape[0]) 5 for i in range(2): data_train.append(data[i]) for i in range(2, 4): data_test.append(data[i]) data_test = np.array(data_test) data_train = np.array(data_train) # 建立model-------------------------------------------------------------------- X_in = Input(shape=(8, ), batch_shape=(739, 8)) X = Reshape((-1, 8))(X_in) node2vec_lstm = LSTM(units=50, input_shape=[739, 8], return_sequences=True)(X) node2vec_lstm_att = attention.attention_3d_block(node2vec_lstm) node2vec_lstm_att = Dense(1)(node2vec_lstm_att) model = Model(X_in, node2vec_lstm_att) model.compile(loss='mse', optimizer='rmsprop') model.summary() # 提取交易额作为ytrain-------------------------------------------------- dataset_total = [] for i in range(len(name_node_pairs)): file = open('./data/temporal link features_5_7days_739\\' + name_node_pairs[i] + '_temp_link_ft.csv') df = pd.read_csv(file) new_data = pd.DataFrame(df, columns=['tran_sum']) dataset = new_data.values # 得到nparray