Get tensorflow DNN layout

This commit is contained in:
2026-02-16 23:03:06 -08:00
parent 99f0d96858
commit 2c6442e421
+67 -15
View File
@@ -1,20 +1,72 @@
import tensorflow as tf import pandas as pd
import keras import numpy as np
from keras.layers import Dense, Flatten, Conv2D import matplotlib.pyplot as plt
from keras import Model import os
from sklearn.model_selection import train_test_split
from keras import Sequential, layers, optimizers
def main(): # Get the CWD for pathing due to being called from C# now
mnist = keras.datasets.mnist SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
DATA_DIR = os.path.join(SCRIPT_DIR, "data")
(x_train, y_train), (x_test, y_test) = mnist.load_data() # Load the dataset
x_train, x_test = x_train / 255.0, x_test / 255.0 dataset = pd.read_parquet(os.path.join(DATA_DIR, "stocks.parquet"))
X = dataset.drop('Volatility_5', axis=1)
Y = dataset['Volatility_5']
# Add a channels dimension # Show the datatypes
x_train = x_train[..., tf.newaxis].astype("float32") print(dataset.dtypes)
x_test = x_test[..., tf.newaxis].astype("float32")
# batch and shuffle the dataset # Split out the test and train
train_ds = tf.data.Dataset.from_tensor_slices( train_features, test_features, train_labels, test_labels = train_test_split(X, Y, test_size=0.2)
(x_train, y_train)).shuffle(10000).batch(32)
test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32) # Create a normalizer to nomralize the data
normalizer = layers.Normalization(axis=-1)
normalizer.adapt(np.array(train_features))
# Start with a linear model
dnn_linear_model = Sequential([
layers.Input(shape=(9,)), # Explicitly tell Keras there are 9 features
normalizer,
layers.Dense(64, activation='relu'),
layers.Dense(64, activation='relu'),
layers.Dense(1)
])
# Configure the model
dnn_linear_model.compile(
optimizer=optimizers.Adam(learning_rate=0.1),
loss='mean_absolute_error'
)
dnn_linear_model.summary()
# Train the model
Training_Data = dnn_linear_model.fit(
train_features,
train_labels,
epochs=100,
# Show progress
verbose=1,
# Calculate validation results on 20% of the training data.
validation_split = 0.2
)
# Predict
test_predictions = dnn_linear_model.predict(test_features).flatten()
a = plt.axes(aspect='equal')
plt.scatter(test_labels, test_predictions)
plt.xlabel('True Values')
plt.ylabel('Predictions')
lims = [0, 50]
plt.xlim(lims)
plt.ylim(lims)
_ = plt.plot(lims, lims)
test_results = dnn_linear_model.evaluate(
test_features, test_labels, verbose=0
)
# Save the model
dnn_linear_model.save(os.path.join(DATA_DIR, "model.keras"))