NLP(一 )文本分类问题

前言

#转为Tensor 数据类型
tf.convert_to_tensor(my_np_array, dtype=tf.float32)
torch.FloatTensor(py_list)
import pandas as pd
def file_process(file_path):
    with open(file_path,'r',encoding='utf_8') as f:
        labels=[]
        texts=[]
        for line in f:
            line=line.split(' ')
            labels.append(line[0])
            texts.append(line[1])     
    return labels,texts
def describe_file(file_path):
    labels,texts=file_process(file_path)
    file_df=pd.DataFrame({'label':labels,'text':texts})
    file_length=file_df['text'].apply(lambda x:len(x))
    return file_df
test_df=describe_file(path)
print(test_df)

#encoding part
!pip install transformers
from transformers import BertTokenizer, TFBertModel
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = TFBertModel.from_pretrained("bert-base-uncased")


test_text=[text for text in test_df['text']]
encoded_input = tokenizer(test_text,padding=True ,return_tensors='tf')#tokenizer 处理(list(str)或者str type)
output = model(encoded_input)

import tensorflow as tf
y_test=[int(label) for label in test_df['label']]
y_train=output['pooler_output']
y_test=tf.convert_to_tensor(y_test, dtype=tf.float32)
# y_test=torch.FloatTensor(y_test)
print(output['pooler_output'].shape,len(y_test))
print(type(y_test),type(y_train))

#model part
from keras.models import Sequential,Model
from keras.layers import LSTM, Dense, Embedding, Dropout,Input
from tensorflow.keras.optimizers import Adam
def build_classifier_model():
  x_input=Input(shape=(768,))
  x_out=Dense(4,activation='relu')(x_input)
  x_out=Dense(1,activation='softmax')(x_out)
  return Model(x_input,x_out)
classifier_model=build_classifier_model()
print(classifier_model.summary())
classifier_model.compile(loss='categorical_crossentropy',
              optimizer=Adam(),
              metrics=['accuracy'])
classifier_model.fit(y_train,y_test,epochs=2)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YingJingh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值