[英]tensorflow2.0 logistic model ouputs nan weight and bias every time
我想知道為什么這個邏輯模型每次都輸出權重和偏差 nan。 有什么可能嗎? (僅供參考/訓練集:2082 行 × 91 列)
import tensorflow as tf
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
features = stock_copy2.iloc[:,:-1]
target = stock_copy2.iloc[:,-1]
x_train, x_test, y_train, y_test = \
train_test_split(features, target, test_size = 0.3, random_state = 1)
tf.random.set_seed(2020)
W = tf.Variable(tf.random.normal([91, 1], mean=0.0))
b = tf.Variable(tf.random.normal([1], mean=0.0))
# Learning Rate
learning_rate = 0.01
# Hypothesis and Prediction Function
def predict(X):
z = tf.matmul(X, W) + b
hypothesis = 1 / (1 + tf.exp(-z))
return hypothesis
# Training
for i in range(2000+1):
with tf.GradientTape() as tape:
hypothesis = predict(x_train)
cost = tf.reduce_mean(-tf.reduce_sum(y_train*tf.math.log(hypothesis) + (1-y_train)*tf.math.log(1-hypothesis)))
W_grad, b_grad = tape.gradient(cost, [W, b])
W.assign_sub(learning_rate * W_grad)
b.assign_sub(learning_rate * b_grad)
if i % 400 == 0:
print(">>> #%s \n Weights: \n%s \n Bias: \n%s \n cost: %s\n" % (i, W.numpy(), b.numpy(), cost.numpy()))
並且輸出是這樣的,除了我設置的拳頭隨機權重和偏差。
Weights:
[[nan]
[nan]
[nan]
[nan]
[nan]
[nan] ....
使用固定學習率進行優化通常不是一個壞主意,因為它在最好的情況下會產生次優的收斂結果,但通常會導致模型發散。 我讓你的代碼使用 RMSprop 優化器收斂(在隨機數據上),它會自動調整你的學習率。
您可以在訓練之前定義一個 keras 優化器,例如
optimizer = tf.keras.optimizers.RMSprop(learning_rate=0.001, rho=0.9)
然后用它來應用你的膠帶漸變:
optimizer.apply_gradients(zip([W_grad, b_grad], [W,b]))
而不是減去梯度。 請查看https://www.tensorflow.org/api_docs/python/tf/keras/optimizers以獲取有關可用優化器模塊的更多信息!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.