簡體   English   中英

類型錯誤:將形狀轉換為 TensorShape 時出錯:int() 參數必須是字符串、類似字節的對象或數字,而不是“元組”。 在蟒蛇

[英]TypeError: Error converting shape to a TensorShape: int() argument must be a string, a bytes-like object or a number, not 'tuple'. in python

運行以下我在網上找到的有關定義機器學習模型的代碼時出現此錯誤:

raise TypeError("Error converting %s to a TensorShape: %s." % (arg_name, e))
TypeError: Error converting shape to a TensorShape: int() argument must be a string, a
bytes-like object or a number, not 'tuple'.

import pandas as pd
import numpy  as np

customers = pd.read_csv('EcommerceCustomers.csv')

X = customers[['Avg. Session Length', 'Time on App', 'Time on Website','Length of Membership']].values
y = customers['Yearly Amount Spent'].values

from sklearn.model_selection import train_test_split

X_training, X_testing, Y_training, Y_testing = train_test_split(X, y, test_size=0.30, random_state=101)

Y_training= np.reshape(Y_training, (-1, 1))
Y_testing= np.reshape(Y_testing, (-1, 1))

from sklearn.preprocessing import MinMaxScaler

X_scaler = MinMaxScaler(feature_range=(0, 1))
Y_scaler = MinMaxScaler(feature_range=(0, 1))

X_scaled_training = X_scaler.fit_transform(X_training)
Y_scaled_training = Y_scaler.fit_transform(Y_training)

X_scaled_testing = X_scaler.fit_transform(X_testing)
Y_scaled_testing = Y_scaler.fit_transform(Y_testing)

print(X_scaled_testing.shape)
print(Y_scaled_testing.shape)

print("Note: Y values were scaled by multiplying by {:.10f} and adding {:.4f}".format(Y_scaler.scale_[0], Y_scaler.min_[0]))

from keras.models import Sequential
from keras.layers import Dense

model = Sequential()
model.add(Dense(50, input_dim=, activation='relu'))
model.add(Dense(100, activation='relu'))
model.add(Dense(50, activation='relu'))
model.add(Dense(1, activation='linear'))
model.compile(loss="mean_squared_error", optimizer="adam")

錯誤發生在這一行:

model.add(Dense(50, input_dim=, activation='relu'))`

這種問題的原因是什么? 我嘗試了很多示例,但找不到解決方案。

在您的代碼中,這一行有一個錯字:

model.add(Dense(50, input_dim=, activation='relu'))

參數input_dim應該是您計划提供給該層的數組的形狀(展平)。 我實際上建議使用input_shape代替。

嘗試這個:

model.add(Dense(50, input_shape=X[0].shape, activation='relu'))

查看keras 參考文檔

此行將導致語法錯誤。 Dense(50, input_dim=, activation='relu')

In [1]: Dense(50, input_dim=, activation='relu')
  File "<ipython-input-2-ed8b4d6f4769>", line 1
    Dense(50, input_dim=, activation='relu')
                        ^
SyntaxError: invalid syntax

您不能在調用keras.layers.Denseinput_dim留空,您必須傳遞input_diminput_shape

model.add(Dense(50, input_dim=(16, ), activation='relu'))

我在 tensorflow 2.0 和新的 keras 上遇到了同樣的問題,我使用了input_dim參數,但我應該做input_shape

model_1.add(Dense(10, activation='relu', input_shape=(50,50,3)))

暫無
暫無

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

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