神经网络学习记录 2022/1/23

昨天家里来了客人,因此没有学习。

今天学习的时候,在用numpy库的transpose时因为自己理解问题,让我想了小半个小时,还好最后转过来弯了。

三维矩阵时我还能理解,问题出现在四维矩阵上:

X_train = np.transpose(X_train.reshape(-1, 3, 32, 32), [0, 2, 3, 1])
print(X_train.shape)

'''
(50000, 32, 32, 3)
'''

按照我当时的理解我以为是按照后面的list交换前面的位置,应该是50000位置不变(0号位),3换到第一个32的位置(2号位),第一个32换到3的位置(1号位),第二个32换到3的位置(1号位)。输出应该是(50000,32,3,32)。

最后才想明白应该是50000的位置(0号位)依然是0号位的数,3的位置(1号位)变成原本2号位上的数字(32),第一个32的位置(2号位)变成原本3号位的数(32),第二个32的位置变成原本1号位的数(3)。这样结果才是(50000,32,32,3)。

下面是今天修改后的代码。softmax损失函数部分参考了书中内容(《深度学习实战》)。

import pickle
import random
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import  OneHotEncoder
%matplotlib inline
plt.rcParams['figure.figsize'] = (10.0, 8.0)
%load_ext autoreload
%autoreload 2
#定义函数

#解压函数
def unpickle(file):
    with open(file, 'rb')as fo:
        dict = pickle.load(fo, encoding='ISO-8859-1')
    return dict

#softmax损失函数
def softmax_loss_vector(W,X,y,reg):
    scores = X.dot(W)
    scores -= np.max(scores, axis =1, keepdims=True)
    scores_E = np.exp(scores)
    sum_scores = np.sum(scores_E, axis = 1, keepdims=True)
    P_scores = scores_E/sum_scores
    J = -(np.sum(y*np.log(P_scores+(1e-7))))/y.shape[0]+0.5*reg*np.sum(W*W)  #平均损失函数
    dW = -np.dot(X.T, y-P_scores)/y.shape[0]+reg*W
    return J,dW
#处理原始数据

#解压数据
labels = []
X_train = []
one_hot = OneHotEncoder()
for i in range(1,6):  #解压训练集
    data = unpickle('./CIAFR-10/cifar-10-batches-py/data_batch_%d'%(i))
    labels.append(data['labels'])
    X_train.append(data['data'])

#训练集变换为ndarray格式
X_train = np.array(X_train, dtype = "float64")
X_train = np.transpose(X_train.reshape(-1, 3, 32, 32), [0, 2, 3, 1])
y_train = np.array(labels).reshape(-1)

#测试集变换为ndarray格式
test = unpickle('./CIAFR-10/cifar-10-batches-py/test_batch')
X_test = np.array(test['data'], dtype = "float64")
X_test = np.transpose(X_test.reshape(-1, 3, 32, 32), [0, 2, 3, 1])

#标签变为onehot
y_train = one_hot.fit_transform(y_train.reshape(-1, 1)).toarray()
y_test = one_hot.transform(np.array(test['labels']).reshape(-1, 1)).toarray()

#数据划分
X_val = []  #样本验证数据
y_val = []
X_sample = []  #样本训练数据
y_sample = []
for i in range(100):  #样本验证数据获取--随机100个
    random_num = random.randint(0,50000-1)
    X_val.append(X_train[random_num])
    y_val.append(y_train[random_num])
for i in range(250):  #样本验证数据获取--随机250个
    random_num = random.randint(0,50000-1)
    X_sample.append(X_train[random_num])
    y_sample.append(y_train[random_num])
X_val = np.array(X_val)
y_val = np.array(y_val)
X_sample = np.array(X_sample)
y_sample = np.array(y_sample)

#数据形状转换
X_train = np.reshape(X_train,(X_train.shape[0],-1))
X_val = np.reshape(X_val,(X_val.shape[0],-1))
X_test = np.reshape(X_test,(X_test.shape[0],-1))
X_sample = np.reshape(X_sample,(X_sample.shape[0],-1))

#归一化——使其在均值附近分布
mean_image = np.mean(X_train,axis = 0)
X_train -= mean_image
X_val -= mean_image
X_test -= mean_image
X_sample -= mean_image

#添加偏置项
X_train = np.hstack((X_train, np.ones((X_train.shape[0], 1))))
X_val = np.hstack((X_val, np.ones((X_val.shape[0], 1))))
X_test = np.hstack((X_test, np.ones((X_test.shape[0], 1))))
X_sample = np.hstack((X_sample, np.ones((X_sample.shape[0], 1))))
#无参数测试
W = np.random.randn(3073,10)*0.0001
loss,dW = softmax_loss_vector(W, X_sample, y_sample, 0.00001)
print(loss)
print(-np.log(0.1))

'''
2.341539374521566
2.3025850929940455
'''

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值