The import numpy as np
The import pandas as pd
The from sklearn. Datasets import load_iris # import data collection
x_data=https://bbs.csdn.net/topics/load_iris (.) data# import feature data collection
y_data=https://bbs.csdn.net/topics/load_iris (.) target# import label data collection
X_train=x_data [: - 30] # characteristics data data training set
X_test=x_data [- 30:] # characteristics data data test set
Y_train=y_data [: - 30] # tag data training set
Y_test=y_data [- 30:] # tag data test set
Np, the random seed (# 1) random seed, keep the consistent order
Np. Random. Shuffle (x_train) # order
Np, the random seed (1)
Np. Random. Shuffle (y_train) # order
Tf. Random. Set_seed (111)
Train_db=tf. Data. The Dataset. From_tensor_slices ((x_train y_train)). The batch # (32) data cut cut the array after a single tensor, the data package
# for the I in train_db:
# print (I)
W=tf. Variable (tf) random) truncated_normal ([4], seed=1)) # initialization parameters, and set for training Variable
B=tf. Variable (tf) random) truncated_normal ([3], seed=1) # initialization parameters, and set for training Variable
Epoch=100
Loss_all=0
Train_loss_result=[]
Test_acc=[]
Lr=0.2
# for I epoch in
For step, (x, y) in enumerate (train_db) :
With tf. GradientTape () as tape:
X=tf. Cast (x, dtype="float64")
# print (x)
W=tf. Cast (w, dtype="float64")
# print (w)
B=tf. Cast (b, dtype="float64")
Y_train=tf. One_hot (tf) cast (y, dtype="int32"), the depth=3)
Y_test=tf. One_hot (tf) cast (y_test, dtype="int32"), the depth=3)
Y_pred=tf. Matmul (x, w) + b
Loss=tf. Nn. Softmax_cross_entropy_with_logits (y_train y_pred)
Loss_mean=tf. Reduce_mean (loss)
# print (loss_mean)
Grad=tape. Gradient (loss_mean, [w, b])
# - lr w=w * grad [0]
# b=b - lr * grad [1]
Print (type (grad [0])
If step==1:
Break
# # loss_all +=loss_mean
Results:
Code above, the loop is, the first step cycle, when calculating the w, b gradient is, there is value, into the cycle is the second time, quoted a null value, thanks to the great god to solve the problem?
CodePudding user response:
What a great god answersCodePudding user response:
Checked the today, know the reason, since changes in the data for float64, b, w viriable type change to tensor, therefore cause data cannot in gradient, parameters could not be updated