Last active
May 20, 2022 15:29
-
-
Save shikanime/94a4235723a50890ca198608de9dde36 to your computer and use it in GitHub Desktop.
Tensorflow sketchbook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
window_size = 2 | |
label_size = 1 | |
inputs = tf.random.stateless_uniform(shape=(10, 3), seed=(2, 3)) | |
dataset = tf.data.Dataset.from_tensor_slices(inputs) | |
dataset = dataset.window(window_size + label_size, shift=label_size, drop_remainder=True) | |
dataset = dataset.flat_map(lambda window: window.batch(window_size + label_size)) | |
dataset = dataset.map(lambda window: (window[:-label_size], window[-label_size])) | |
for x, y in dataset: | |
print(x.numpy(), y.numpy()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
fft = tf.signal.rfft(df['T (degC)']) | |
f_per_dataset = np.arange(0, len(fft)) | |
n_samples_h = len(df['T (degC)']) | |
hours_per_year = 24*365.2524 | |
years_per_dataset = n_samples_h/(hours_per_year) | |
f_per_year = f_per_dataset/years_per_dataset | |
plt.step(f_per_year, np.abs(fft)) | |
plt.xscale('log') | |
plt.ylim(0, 400000) | |
plt.xlim([0.1, max(plt.xlim())]) | |
plt.xticks([1, 365.2524], labels=['1/Year', '1/day']) | |
_ = plt.xlabel('Frequency (log scale)') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
metrics = pd.DataFrame(history.history) | |
metrics.plot(xlabel="epochs", subplots=True, legend=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
window_size = 2 | |
split_time = 1000 | |
forecast = range(len(series) - window_size) | |
forecast = map(lambda x: model.predict(series[x:x + window_size][np.newaxis]), forecast) | |
forecast = np.fromiter(forecast, dtype=np.float64) | |
forecast = forecast[split_time-window_size:] | |
forecast = np.array(forecast)[:, 0, 0] | |
plt.plot(time, forecast) | |
plt.xlabel("Time") | |
plt.ylabel("Value") | |
plt.grid(True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
window_size = 2 | |
label_size = 1 | |
inputs = tf.random.stateless_uniform(shape=(10, 3), seed=(2, 3)) | |
labels = inputs[:, 2] | |
inputs = tf.keras.utils.timeseries_dataset_from_array(inputs, None, sequence_length=window_size, sequence_stride=window_size+label_size) | |
labels = tf.keras.utils.timeseries_dataset_from_array(labels, None, sequence_length=label_size, sequence_stride=window_size+label_size, start_index=window_size) | |
dataset = tf.data.Dataset.zip((inputs, labels)) | |
for x, y in dataset: | |
print(x.numpy(), y.numpy()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment