2023-03-02干活小计

学会了dataset dataloader的实现:

import numpy as np


class Model:
    def __init__(self):
        self.model =  np.random.normal(size=(max_len, 1))
    def forward(self, text_index):
        pre = text_index @ self.model
        return pre

class Dhl_Dataset:
    def __init__(self, all_text, all_label, max_len, batch_size, shuffle, word_2_index):
        self.all_text = np.array(all_text)
        self.word_2_index = word_2_index
        self.all_label = np.array(all_label)
        self.max_len = max_len
        assert len(self.all_text) == len(self.all_label), print("数据标签长度不等!")
        self.batch_size = batch_size
        self.shuffle = shuffle

    def __iter__(self):
        return Dhl_DataLoader(self)

    def __len__(self):
        return len(self.all_text)

class Dhl_DataLoader:
    def __init__(self, dataset):
        self.dataset = dataset
        self.shuffle_index = np.array([i for i in range(0, len(self.dataset))])
        self.cursor = 0
        if self.dataset.shuffle == True:
            np.random.shuffle(self.shuffle_index)

    def __getitem__(self, index):
        text = self.dataset.all_text[index][:self.dataset.max_len]
        label =self.dataset.all_label[index]
        text_index = [self.dataset.word_2_index[word] for word in text]
        text_index = text_index + [0] * (self.dataset.max_len - len(text_index))
        return np.array(text_index), np.array(label)



    def __next__(self):
        if self.cursor >= len(self.dataset):
            raise StopIteration
        index = self.shuffle_index[self.cursor:self.cursor + self.dataset.batch_size]
        batch_text_index = []
        batch_label = []
        for i in index:
            text_index, label = self[i]
            batch_text_index.append(text_index)
            batch_label.append(label)

        self.cursor += self.dataset.batch_size
        return batch_text_index, batch_label


def read_data(file_path):
    with open(file_path, "r", encoding="utf-8") as f:
        all_text = []
        all_label = []
        all_data = f.read().split("\n")
        for line in all_data:
            text, label = line.split(" ")
            all_text.append(text)
            all_label.append(int(label))
        return all_text, all_label

def build_word_2_index(all_text):
    word_2_index = {"<PAD>": 0}
    for text in all_text:
        for word in text:
            word_2_index[word] = word_2_index.get(word, len(word_2_index))
            #if word not in word_2_index:

                #word_2_index[word] = len(word_2_index)
    return word_2_index

if __name__ == "__main__":

    train_text, train_label = read_data("..//not_that_easy//data//train.txt")
    assert len(train_text) == len(train_label), print("文本数量和标签数量不等!")
    print(f"加载数据成功,长度为:{len(train_text)}")
    word_2_index = build_word_2_index(train_text)
    epoch = 10
    batch_size = 3
    max_len = 10
    shuffle = True
    train_dataset = Dhl_Dataset(train_text, train_label, max_len, batch_size, shuffle, word_2_index)
    model = Model()
    for e in range(epoch):
        print(f"this is the {e}th epoch")
        for batch_text_index, batch_label in train_dataset:
            pre = model.forward(batch_text_index)
            print(pre)

学习了yield的使用

简单的pandas:

我他妈碰到了一个傻逼bug红红火火恍恍惚惚,

AttributeError: module 'pandas' has no attribute 'read_csv'

因为爷把一个py文件命名为pandas.py 哈哈哈哈笑死我了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值