第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

學(xué)習(xí)三次方程

學(xué)習(xí)三次方程

慕虎7371278 2023-06-13 11:06:36
我的模型在完成訓(xùn)練后出現(xiàn)了很高的錯(cuò)誤。正確選擇節(jié)點(diǎn)和層數(shù)是否非常關(guān)鍵,還是我需要選擇更慢的學(xué)習(xí)率或更多的 epoch?我原以為為這個(gè)方程式訓(xùn)練模型不會(huì)有什么大不了的。# f(x) = 0.22*x*x*x-0.6*x*x+0.4*x-0.5import tensorflow as tfimport numpy as npimport logginglogger = tf.get_logger()logger.setLevel(logging.ERROR)x = np.array([-5,-4.8,-4.6,-4.4,-4.2,-4,-3.8,-3.6,-3.4,-3.2,-3,-2.8,-2.6,-2.4,-2.2,-2,-1.8,-1.6,-1.4,-1.2,-1,-0.8,-0.6,-0.4,-0.2,0,0.2,0.4,0.6,0.8,1,1.2,1.4,1.6,1.8,2,2.2,2.4,2.6,2.8,3,3.2,3.4,3.6,3.8,4,4.2,4.4,4.6,4.8,5],? dtype=float)y = np.array([-45,-40.57424,-36.44992,-32.61648,-29.06336,-25.78,-22.75584,-19.98032,-17.44288,-15.13296,-13.04,-11.15344,-9.46272,-7.95728,-6.62656,-5.46,-4.44704,-3.57712,-2.83968,-2.22416,-1.72,-1.31664,-1.00352,-0.77008,-0.60576,-0.5,-0.44224,-0.42192,-0.42848,-0.45136,-0.48,-0.50384,-0.51232,-0.49488,-0.44096,-0.34,-0.18144,0.04528,0.350720000000001,0.745439999999999,1.24,1.84496,2.57088,3.42832,4.42784,5.58,6.89536,8.38448,10.05792,11.92624,14],? dtype=float)for i,c in enumerate(x):? print("x = {}, y = {}".format(x[i], y[i]))l0 = tf.keras.layers.Dense(units=4, input_shape=[1])l1 = tf.keras.layers.Dense(units=10)l2 = tf.keras.layers.Dense(units=1)model = tf.keras.Sequential([l0, l1, l2])model.compile(loss='mean_squared_error', optimizer=tf.keras.optimizers.Adam(0.1))history = model.fit(x, y, epochs=50000, verbose=False)print("Finished training the model")import matplotlib.pyplot as pltplt.xlabel('Epoch Number')plt.ylabel("Loss Magnitude")plt.plot(history.history['loss'])
查看完整描述

1 回答

?
揚(yáng)帆大魚(yú)

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超9個(gè)贊

您錯(cuò)過(guò)了在模型中添加激活函數(shù)。對(duì)于任何逼近非線性函數(shù)的神經(jīng)網(wǎng)絡(luò),非線性激活函數(shù)是必需的。

一個(gè)神經(jīng)元(沒(méi)有激活的線性回歸單元)可以表示為

http://img1.sycdn.imooc.com//6487dd7d0001b03702900040.jpg

其中,f(x; W,b)表示由W 和 b參數(shù)化的x的函數(shù)。

考慮單個(gè)輸入、2 個(gè)隱藏神經(jīng)元、單個(gè)輸出網(wǎng)絡(luò):

隱藏的神經(jīng)元是:n1 = w1.x + b1** and **n2 = w2.x + b2

輸出神經(jīng)元是:n3 = w3.n1 + w4.n2 + b3

代入n1n2輸入輸出神經(jīng)元:

n3 = w3(w1.x + b1) + w4(w2.x + b2) + b3**

n3 = w1w3x + b1 + w2w4x + b2 + b3**

n3 = x(w1w3 + w2w4) + (b1 + b2 + b3)

n3 = Wx + B

其中W 和 B是常數(shù)W = w1w3+w2w4B = b1+b2+b3。

所以最后這neural network歸結(jié)為一個(gè)Linear regression unit。因此,這不能逼近任何非線性函數(shù),因?yàn)榫€性方程的線性組合始終是線性方程。

所以只需要引入隱藏神經(jīng)元的激活函數(shù)。它會(huì)起作用的。

這是修改后的代碼:

# f(x) = 0.22*x*x*x-0.6*x*x+0.4*x-0.5

import tensorflow as tf

import numpy as np

import logging


logger = tf.get_logger()

logger.setLevel(logging.ERROR)

x = np.array([-5,-4.8,-4.6,-4.4,-4.2,-4,-3.8,-3.6,-3.4,-3.2,-3,-2.8,-2.6,-2.4,-2.2,-2,-1.8,-1.6,-1.4,-1.2,-1,-0.8,-0.6,-0.4,-0.2,0,0.2,0.4,0.6,0.8,1,1.2,1.4,1.6,1.8,2,2.2,2.4,2.6,2.8,3,3.2,3.4,3.6,3.8,4,4.2,4.4,4.6,4.8,5],  dtype=float)

y = np.array([-45,-40.57424,-36.44992,-32.61648,-29.06336,-25.78,-22.75584,-19.98032,-17.44288,-15.13296,-13.04,-11.15344,-9.46272,-7.95728,-6.62656,-5.46,-4.44704,-3.57712,-2.83968,-2.22416,-1.72,-1.31664,-1.00352,-0.77008,-0.60576,-0.5,-0.44224,-0.42192,-0.42848,-0.45136,-0.48,-0.50384,-0.51232,-0.49488,-0.44096,-0.34,-0.18144,0.04528,0.350720000000001,0.745439999999999,1.24,1.84496,2.57088,3.42832,4.42784,5.58,6.89536,8.38448,10.05792,11.92624,14],  dtype=float)

for i,c in enumerate(x):

  print("x = {}, y = {}".format(x[i], y[i]))


#######################added activation#####################################

l0 = tf.keras.layers.Dense(units=4, activation='relu', input_shape=[1])

l1 = tf.keras.layers.Dense(units=10, activation='relu',)

############################################################################

l2 = tf.keras.layers.Dense(units=1)

model = tf.keras.Sequential([l0, l1, l2])


model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=1e-1), loss='mean_squared_error', metrics=['mean_squared_error'])


history = model.fit(x, y, epochs=10000, verbose=1)

print("Finished training the model")

import matplotlib.pyplot as plt

plt.xlabel('Epoch Number')

plt.ylabel("Loss Magnitude")

plt.plot(history.history['loss'])

plt.show()


print("For x = -5 (nom = -45), y = {} ".format(model.predict([-5]))) # should be -45

print("For x = -3 (nom = -13.4), y = {} ".format(model.predict([-3]))) # should be -13.4

print("For x = -2.4 (nom = -7.96), y = {} ".format(model.predict([-2.4]))) # should be -7.96

print("For x = -1.4 (nom = -2.84), y = {} ".format(model.predict([-1.4]))) # should be -2.84

print("For x = 0 (nom = -0.5), y = {} ".format(model.predict([0]))) # should be -0.5

print("For x = 1.2 (nom = -0.50), y = {} ".format(model.predict([1.2]))) # should be -0.50

print("For x = 3.4 (nom = 2.57), y = {} ".format(model.predict([3.4]))) # should be 2.57

print("For x = 4.8 (nom = 11.93), y = {} ".format(model.predict([4.8]))) # should be 11.93


print("Model predicts that for x = 3.18, y = {} ".format(model.predict([3.18])))

以下是結(jié)果:


    .

    .

    .

    .

Epoch 9997/10000

2/2 [==============================] - 0s 2ms/step - loss: 0.2931 - mean_squared_error: 0.2931

Epoch 9998/10000

2/2 [==============================] - 0s 997us/step - loss: 0.2993 - mean_squared_error: 0.2993

Epoch 9999/10000

2/2 [==============================] - 0s 498us/step - loss: 0.2872 - mean_squared_error: 0.2872

Epoch 10000/10000

2/2 [==============================] - 0s 498us/step - loss: 0.2598 - mean_squared_error: 0.2598

Finished training the model

For x = -5 (nom = -45), y = [[-44.53207]]

For x = -3 (nom = -13.4), y = [[-12.643011]]

For x = -2.4 (nom = -7.96), y = [[-8.224297]]

For x = -1.4 (nom = -2.84), y = [[-0.7255349]]

For x = 0 (nom = -0.5), y = [[-0.7255349]]

For x = 1.2 (nom = -0.50), y = [[-0.7255349]]

For x = 3.4 (nom = 2.57), y = [[2.8460937]]

For x = 4.8 (nom = 11.93), y = [[12.013783]]

Model predicts that for x = 3.18, y = [[1.9653977]]

損耗曲線:

http://img1.sycdn.imooc.com//6487dd690001780906340468.jpg

查看完整回答
反對(duì) 回復(fù) 2023-06-13
  • 1 回答
  • 0 關(guān)注
  • 104 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)