Beispiel #1
0
    def call(self, node, adj, batch, edge_attr, training=True):
        feature = tf.nn.embedding_lookup(self.embedding, node)
        predict = self.model(feature, adj, batch, edge_attr, training=training)
        return predict

    def predict(self, nodes, adj, batch, edge_attr, training=False):
        return self(nodes, adj, batch, edge_attr, training)


accs_all = []
for i in range(10):
    model = TextSAGEynamicWeight(dim, num_class, drop_rate)
    optimize = tf.optimizers.Adam(lr)

    cross_entropy = Losess.MaskSparseCategoricalCrossentropy()
    acc_score = Metric.SparseAccuracy()

    stop_monitor = EarlyStopping(monitor="loss",
                                 patience=10,
                                 restore_best_weights=False)
    for i in range(epoch):
        t = time.time()

        for node, label, adj, edge_attr, batch in data.load(nodes[:-500],
                                                            adjs[:-500],
                                                            labels[:-500],
                                                            edge_attrs[:-500],
                                                            batchs[:-500],
                                                            batch_size=32):
            node, label, adj, edge_attr, batch = merge_batch_graph(
                node, label, adj, edge_attr, batch)
Beispiel #2
0
model.build(input_shape=(3, param.batch_size, param.maxlen))

model.summary()

# 写入数据 通过check_exist=True参数控制仅在第一次调用时写入
writer = TFWriter(param.maxlen, vocab_file,
                    modes=["valid"], check_exist=False)

ner_load = TFLoader(param.maxlen, param.batch_size, epoch=3)

# Metrics
f1score = Metric.SparseF1Score(average="macro")
precsionscore = Metric.SparsePrecisionScore(average="macro")
recallscore = Metric.SparseRecallScore(average="macro")
accuarcyscore = Metric.SparseAccuracy()

# 保存模型
checkpoint = tf.train.Checkpoint(model=model)
checkpoint.restore(tf.train.latest_checkpoint('./save'))
# For test model
Batch = 0
f1s = []
precisions = []
recalls = []
accuracys = []
for X, token_type_id, input_mask, Y in ner_load.load_valid():
    predict = model.predict([X, token_type_id, input_mask])  # [batch_size, max_length,label_size]
    # predict = tf.argmax(output, -1)
    f1s.append(f1score(Y, predict))
    precisions.append(precsionscore(Y, predict))
Beispiel #3
0
model = BERT_NER(param)

model.build(input_shape=(4, param.batch_size, param.maxlen))

model.summary()

# 写入数据 通过check_exist=True参数控制仅在第一次调用时写入
writer = TFWriter(param.maxlen, vocab_file, modes=["valid"], check_exist=True)

ner_load = TFLoader(param.maxlen, param.batch_size)

# Metrics
f1score = Metric.SparseF1Score("macro", predict_sparse=True)
precsionscore = Metric.SparsePrecisionScore("macro", predict_sparse=True)
recallscore = Metric.SparseRecallScore("macro", predict_sparse=True)
accuarcyscore = Metric.SparseAccuracy(predict_sparse=True)

# 保存模型
checkpoint = tf.train.Checkpoint(model=model)
checkpoint.restore(tf.train.latest_checkpoint('./save'))
# For test model
# print(dir(checkpoint))
Batch = 0
f1s = []
precisions = []
recalls = []
accuracys = []
for X, token_type_id, input_mask, Y in ner_load.load_valid():
    predict = model.predict([X, token_type_id, input_mask,
                             Y])  # [batch_size, max_length,label_size]