import tensorflow as tf
import numpy as np
from tqdm import tqdm
from datasets import load_dataset
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras import layers, Input
import random
from datetime import datetime, timedelta
from datasets import load_dataset
from datasets import load_dataset
dataset = load_dataset("csv", data_files="bitcoin_daily/data_btc.csv", split="train")
input_shape = (3, 6)
def GetRandomThirtyDays(dataset):
dates = dataset["date"]
random_date = random.choice(dates)
# Find the index of the random date
random_date_index = dates.index(random_date)
# Get the last 30 days starting from the random date
start_index = max(0, random_date_index - 5) # Ensure the start index is not negative
last_30_days_dates = dates[start_index:random_date_index]
# Check if all dates exist in the dataset
for date in last_30_days_dates:
if date not in dates:
return GetRandomThirtyDays() # If any date is missing, recursively call the function to get a new random set of 30 days
# Save the indices of the last 30 days
last_30_days_indices = list(range(start_index, random_date_index + 1))
return last_30_days_indices, random_date_index + 1
def VerifyIndexExists(dataset, index):
try:
dataset["price"][index]
return dataset["price"][index]
except:
return False
def GetSellPrices(dataset, indices, ):
# Get the sell prices for the last 30 days using the indices
DataDictionary = []
Destroyed = False
for index in indices:
# Get the sell price, market_caps and the volumes for the current index IF it exists and return it. If anything is nil (destroyed)
try:
if dataset["price"][index] == None:
Destroyed = True
break
sell_price = dataset["price"][index]
if dataset["market_caps"][index] == None:
Destroyed = True
break
market_cap = dataset["market_caps"][index]
if dataset["total_volumes"][index] == None:
Destroyed = True
break
volume = dataset["total_volumes"][index]
DataDictionary.append([sell_price, market_cap, volume])
except:
Destroyed = True
return DataDictionary, Destroyed
def Get30DayData(dataset):
indices, answerIndex = GetRandomThirtyDays(dataset) #Get 30 random dates
ThirtyDayDataDictionary, Destroyed = GetSellPrices(dataset, indices)
# go back recursively if we are destroyed
PredictionAnswer = VerifyIndexExists(dataset, answerIndex)
if not PredictionAnswer:
return Get30DayData(dataset)
if Destroyed:
return Get30DayData(dataset)
return ThirtyDayDataDictionary, PredictionAnswer
model = tf.keras.Sequential([
layers.Input(shape=input_shape), # Input layer with specified shape
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dense(10, activation='softmax'),
layers.Dense(1, activation='sigmoid')
])
model.compile(optimizer="adam", loss="mean_squared_error", metrics=["accuracy"])
X_training = []
answer = []
for _ in tqdm(range(20)):
X, y = Get30DayData(dataset)
if X:
X_training.append(X)
answer.append(y)
X_training_np = np.array(X_training)
answer = np.array(X_training)
model.fit(X_training_np, answer, epochs=300, batch_size=1, verbose=0)
model.evaluate(X_training_np, answer, verbose=2)
This is my code and I’m getting multiple random shape errors. I am a tensorflow beginner. The reason Im only using the past 5 days of data is because it’s easier to read as a log. Help would be appreciated!
Im trying to train an ANN on a 3 by 5 list to capture all the data of the past days using a float as the prediction.
New contributor
Dylan Bates is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.