我开始研究深度学习。编写一些代码时出现问题。
请让我知道解决问题。
我不知道要解决它。
我安装了tensorflow-cpu
。这是个问题吗?我需要图形卡吗?
-我的错误-
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type int).
-代码`-
import numpy as np
import pandas as pd
samsung=pd.read_csv(r"C:\Users\XNOTE\Desktop\Deepstudy\pred
test\samsung.csv", index_col=0, header=0, encoding='cp949', sep=',')
for i in range(len(samsung.index)):
for j in range(len(samsung.iloc[i])):
samsung.iloc[i,j] = int(samsung.iloc[i,j].replace(',',''))
samsung=samsung.sort_values(['일자'], ascending=[True])
samsung=samsung.values
# code split
def split_xy5(dataset, time_steps, y_column):
x,y=list(), list()
for i in range(len(dataset)):
x_end_number=i+time_steps
y_end_number=x_end_number + y_column
if y_end_number > len(dataset):
break
tmp_x = dataset[i:x_end_number, :]
tmp_y = dataset[x_end_number : y_end_number, 3]
x.append(tmp_x)
y.append(tmp_y)
return np.array(x), np.array(y)
x,y = split_xy5(samsung, 5, 1)
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=1, test_size = 0.3)
x_train = np.reshape(x_train,(x_train.shape[0], x_train.shape[1] * x_train.shape[2]))
x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1]*x_test.shape[2]))
print(x_train.shape)
print(x_test.shape)
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.fit(x_train)
x_train_scaled = scaler.transform(x_train)
x_test_scaled = scaler.transform(x_test)
print(x_train_scaled[0,:])
from keras.models import Sequential
from keras.layers import Dense
model = Sequential()
model.add(Dense(64, input_shape = (25, )))
model.add(Dense(32, activation = 'relu'))
model.add(Dense(32, activation = 'relu'))
model.add(Dense(32, activation = 'relu'))
model.add(Dense(32, activation = 'relu'))
model.add(Dense(1))
model.compile(loss='mse', optimizer='adam', metrics=['mse'])
from keras.callbacks import EarlyStopping
early_stopping = EarlyStopping(patience=20)
model.fit(x_train_scaled, y_train, validation_split=0.2,
verbose=1, batch_size=1, epochs=100, callbacks=[early_stopping])
loss, mse = model.evaluate(x_test_scaled, y_test, batch_size=1)
print('loss : ', loss)
print('mse : ', mse)
y_pred = model.predict(x_test_scaled)
for i in range(5):
print('종가 : "', y_test[i], '/ 예측가 : ', y_pred[i])
加载文件并使数组使用numpy。 制作模型,但不起作用 引用
答案 0 :(得分:0)
假设x_train_scaled
的类型为int,而您在model.fit上遇到错误,则可以尝试以下操作:
tf.cast(x_train_scaled, tf.int32)
您可以对y_train和其他测试数组执行相同的操作,并将其转换为所需的tensorflow dtype。