簡體   English   中英

簡單的單層神經網絡

[英]Simple single layer neural network

我正在寫一個非常簡單的網絡:

import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np

training_data = np.array([[1, 1, 1], [2, 3, 1], [0, -1, 4], [0, 3, 0], [10, -6, 8], [-3, -12, 4]])
testing_data = np.array([6, 11, 1, 9, 10, -38])

model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(units = 1, activation = tf.keras.activations.relu, input_shape = (3, )))
model.compile(optimizer = tf.keras.optimizers.RMSprop(0.001), loss = tf.keras.losses.mean_squared_error, metrics = tf.keras.metrics.mean_squared_error)
model.summary()
model.fit(training_data, testing_data, epochs = 1, verbose = 'False')
print("Traning completed.")
model.predict(np.array([1, 1, 1]))

目標是訓練權重,如:aX + bY + cZ =(輸出)
但我得到了錯誤

ValueError:層序 54 的輸入 0 與層不兼容:輸入形狀的預期軸 -1 具有值 3,但接收到形狀為 [None,1] 的輸入


我無法制作維度場景,我做錯了什么? 有什么幫助嗎?

在 Keras 中,當您指定輸入形狀批量大小時忽略,請參閱此處了解更多詳細信息。 您對input_shape = (3, )的聲明是正確的,但是當您進行推斷時,您還需要通過添加一個額外的維度來考慮批量大小,而不是np.array([1, 1, 1])你需要有np.array([[1, 1, 1]])

import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np

training_data = np.array([[1, 1, 1], [2, 3, 1], [0, -1, 4], [0, 3, 0], [10, -6, 8], [-3, -12, 4]])
testing_data = np.array([6, 11, 1, 9, 10, -38])

model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(units = 1, activation = tf.keras.activations.relu, input_shape = (3,)))
model.compile(optimizer = tf.keras.optimizers.RMSprop(0.001), loss = tf.keras.losses.mean_squared_error, metrics = [tf.keras.metrics.mean_squared_error])
model.summary()

model.fit(training_data, testing_data, epochs = 1, verbose = 'False')
print("Traning completed.")
model.predict(np.array([[1, 2, 1]]))

array([[0.08026636]], dtype=float32)

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM