gpt4 book ai didi

python - 值错误 : Failed to convert a NumPy array to a Tensor (Unsupported object type numpy. ndarray)。试图预测特斯拉股票

转载 作者:行者123 更新时间:2023-12-03 23:06:04 37 4
gpt4 key购买 nike

最后你可以看到我已经尝试将它转换为一个 numpy 数组,但我不明白为什么 tensorflow 不支持它?我查看了其他相关页面,但似乎没有任何帮助。为了正确地适应模型,我还需要对数据做一些其他的格式吗?
这就是 keras 所说的:
X
训练数据的向量、矩阵或数组(如果模型有多个输入,则为列表)。如果模型中的所有输入都已命名,您还可以传递一个将输入名称映射到数据的列表。如果从框架原生张量(例如 TensorFlow 数据张量)馈送,则 x 可以为 NULL(默认)。

目标(标签)数据的向量、矩阵或数组(如果模型有多个输出,则为列表)。如果模型中的所有输出都已命名,您还可以传递一个将输出名称映射到数据的列表。如果从框架原生张量(例如 TensorFlow 数据张量)馈送,则 y 可以为 NULL(默认)。

import pandas as pd
from sklearn import preprocessing
from collections import deque
import numpy as np
import random as rd
import time
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, LSTM, BatchNormalization



data = pd.read_csv("TSLA.csv")

data.set_index("Date", inplace=True)
data = data[["Close", "Volume"]]

Back_period_history = 100
Future_predict = 10


def classify(current, future):
if float(future) > float(current):
return 1
else:
return 0


data["future"] = data["Close"].shift(-Future_predict)
data["target"] = list(map(classify, data["Close"], data["future"]))


#print(data.head(20))

times = sorted(data.index.values)
last_10pct = times[-int(0.1*len(times))]

validation_data = data[(data.index >= last_10pct)]
data = data[(data.index < last_10pct)]

def preproccesing(data):
data = data.drop("future", 1)

for col in data.columns:
if col != "target":
data[col] = data[col].pct_change()
data.dropna(inplace=True)
data[col] = preprocessing.scale(data[col].values)
data.dropna(inplace = True)

sequential_data = []
prev_days = deque(maxlen=Back_period_history)
for i in data.values:
prev_days.append([n for n in i[:-1]])
if len(prev_days) == Back_period_history:
sequential_data.append([np.array(prev_days), i[-1]])

rd.shuffle(sequential_data)

buys = []
sells = []

for seq, target in sequential_data:
if target == 0:
sells.append([seq, target])
elif target == 1:
buys.append([seq,target])

rd.shuffle(buys)
rd.shuffle(sells)

lower = min(len(buys), len(sells))

buys = buys[:lower]
sells = sells[:lower]

sequential_data = buys+sells

rd.shuffle(sequential_data)

X = []
y = []

for seq, target in sequential_data:
X.append(sequential_data)
y.append(target)

return np.array(X),y


train_x, train_y = preproccesing(data)
validation_x, validation_y = preproccesing(validation_data)

model = Sequential()

model.add(LSTM(
128, input_shape = (train_x.shape[1:]), activation = "relu", return_sequences = True
))
model.add(Dropout(0.2))
model.add(BatchNormalization())

model.add(LSTM(
128, input_shape = (train_x.shape[1:]), activation = "relu", return_sequences = True
))
model.add(Dropout(0.2))
model.add(BatchNormalization())

model.add(LSTM(
128, input_shape = (train_x.shape[1:]), activation = "relu", return_sequences = True
))
model.add(Dropout(0.2))
model.add(BatchNormalization())

model.add(Dense(32, activation = "relu"))
model.add(Dropout(0.2))

model.add(Dense(2, activation = "softmax"))

opt = tf.keras.optimizers.Adam()

model.compile(loss="mse", optimizer=opt, metrics=["accuracy"])

train_x = np.asarray(train_x)
train_y = np.asarray(train_y)
validation_x = np.asarray(validation_x)
validation_y = np.asarray(validation_y)

history = model.fit(train_x, train_y, batch_size = 64, epochs = 7, validation_data = (validation_x, validation_y))```


最佳答案

这有效tf.convert_to_tensor(y)

关于python - 值错误 : Failed to convert a NumPy array to a Tensor (Unsupported object type numpy. ndarray)。试图预测特斯拉股票,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62641203/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com