![](/img/trans.png)
[英]TypeError: Error converting shape to a TensorShape: int() argument must be a string or a number, not 'tuple'
[英]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'))
此行將導致語法錯誤。 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.Dense
將input_dim
留空,您必須傳遞input_dim
或input_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.