簡體   English   中英

我正在嘗試使用 Keras 實現神經網絡。 我的錯誤如下:

[英]I am trying to implement a neural network using Keras. My error is listed below:

錯誤:層序 9 的輸入 0 與層不兼容:輸入形狀的預期軸 -1 具有值 2,但接收到形狀為 (25, 1) 的輸入

輸入條件:隨機兩組數據集,每組有50個數據模式,二維x1和x2。 一個數據集有 0 <= x1 <= 3 和 0 <= x2 <= 3,目標為 0,第二個數據集有 6 <= x1 <= 9 和 6 <= x2 <= 9,目標為 1。

以下是我的代碼:

import tensorflow as tf
import numpy as np
import random
from tensorflow import keras
from keras.models import Sequential
from keras.layers import Dense

model = tf.keras.Sequential([keras.layers.Dense(units=4, input_shape=[2])])
model.add(Dense(units= 3, activation='relu'))
model.add(Dense(units= 1, activation='sigmoid'))
model.compile(optimizer='sgd', loss='mean_squared_error')

x1 = list(np.random.uniform(0,3,50))
x2 = list(np.random.uniform(0,3,50))
labels = [0 for i in range(50)]                                                 #target is set  to 0

                                                                                #concatenating the list with tother condition of <6<=9
x1 = x1+ list(np.random.uniform(6,9,50))                                        #Plotting the points between 6 to 9 and putting 50 random points 
x2 += list(np.random.uniform(6,9,50))
labels += [1 for i in range(50)]
x = np.append(x1, x2, axis= -1)
print (x)

x_train = np.array(x, dtype='float64')
tr = np.array(t, dtype='float64'

)

我也嘗試過其他可能的語法,例如:

x1 = []

x2 = []
t = []
for i in range(50):
  x1.append(random.uniform(0,3))
  x2.append(random.uniform(0,3))
for i in range(50):
  x1.append(random.uniform(6,9))
  x2.append(random.uniform(6,9))
for i in range(50):
  t.append(0)
for i in range(50):
  t.append(1)

x1 = np.random.uniform([0,3,size=(50,2))
x2 = np.random.uniform([6,9,size=(50,2))

它在最后一行顯示錯誤,即 (model.fit) 行。

有很多事情我不明白。 你有 no.fit() 行,所以我不知道你在問什么。 您反復創建 arrays 然后覆蓋它們。 您還有幾個未使用的變量(為什么?)。 最后你從 keras 和 tensorflow.keras 導入,這些是完全獨立的包。 這是我認為您正在嘗試做的嘗試:

import tensorflow as tf, numpy as np

#Data
X = np.random.random((10_000,2)) #10_000 2-unit vectors. 
Y = np.random.random((10_000,))  #10_000 numbers. 

#Model
model = tf.keras.Sequential([
    tf.keras.layers.Dense(4),
    tf.keras.layers.Dense(3,'relu'),
    tf.keras.layers.Dense(1,'sigmoid'),
])
model.compile('sgd','mse')

#Training
model.fit(X,Y,256,10)

暫無
暫無

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

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