读.txt数据并合并~读npy数据并合并

# -*- coding:utf-8 -*-
import os
import numpy as np

'''
def weight_bias():
    value = []
    fh = open(weight_path, 'r')
    for line in fh:
        line = line.rstrip()
        line = line.strip('\n')
        words = line.split()
        value.append(words[0])

    fh = open(bias_path, 'r')
    for line in fh:
        line = line.rstrip()
        line = line.strip('\n')
        words = line.split()
        value.append(words[0])
    print(len(value))

    with open(out_path, 'w')as f:
        for i in range(len(value)):
            val = '%.8f'%(float(value[i]))
            f.write(val+',')
            if (i+1)%10==0:
                f.write('\n')
    f.close()

if __name__ == '__main__':
    weight_path = "./_Conv2d_weights.txt"
    bias_path = "./_Conv2D_bias.txt"
    out_path = "./out.txt"
    weight_bias()
'''

def weight_bias():
    weight_data = read_img_from_npy(weight_path)
    bias_data = read_img_from_npy(bias_path)
    bias_data = 0.00006179179035825655 * bias_data
    value = []
    for i in range(weight_data.shape[0]):
                for j in range(weight_data.shape[1]):
                        for s in range(weight_data.shape[2]):
                                for t in range(weight_data.shape[3]):
                                        val = 0.0026262556202709675 * (weight_data[i,j,s,t] - 126)
                                        value.append(val)
    value.append(bias_data[0])

    print(len(value))
    
    with open(out_path, 'w')as f:
        for i in range(len(value)):
            val = '%.8f'%value[i]
            f.write(val+',')
            if (i+1)%10==0:
                f.write('\n')
    f.close()

def read_img_from_npy(weight_path):
    datas = np.load(weight_path)
    return datas

if __name__ == '__main__':
    weight_path = "./_Conv2d_weight.npy"
    bias_path = "./_Conv2d_bias.npy"
    out_path = "./out0902.txt"
    weight_bias()

  

posted @ 2021-09-02 09:40  皮卡皮卡妞  阅读(73)  评论(0编辑  收藏  举报