This file contains hidden or 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 tensorflow as tf | |
import gymnasium as gym | |
# Create the CartPole Environment | |
env = gym.make('CartPole-v1') | |
# Define the actor and critic networks | |
actor = tf.keras.Sequential([ | |
tf.keras.layers.Dense(32, activation='relu'), |
This file contains hidden or 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
from keras.layers import Dense, Flatten, InputLayer, Reshape | |
from keras.models import Sequential | |
from keras.datasets import mnist | |
import numpy as np | |
import matplotlib.pyplot as plt | |
(x_train, y_train), (x_test, y_test) = mnist.load_data() | |
x = np.concatenate([x_train, x_test]) / 255. |
This file contains hidden or 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
//mod function that handles negatives "properly" | |
function mod(a, b) | |
{ | |
return ((a%b)+b)%b | |
} | |
//shifts a tensor n times on the given axis | |
function shift(x, axis, n) | |
{ | |
n = mod(n, x.shape[axis]); |
This file contains hidden or 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
//this example was tested in electron; if anyone knows how to get around | |
//this silliness when using nodejs/electron (without including the .js file in | |
//html, please let me know) | |
var tfCore = require("@tensorflow/tfjs-core"); | |
var tfLayers = require("@tensorflow/tfjs-layers"); | |
var tf = Object.assign({}, tfCore, tfLayers); | |
var xs = tf.tensor([[0, 0], [0, 1], [1, 0], [1, 1]]); | |
var ys = tf.tensor([[0], [1], [1], [0]]); |
This file contains hidden or 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
var tf = require("@tensorflow/tfjs"); | |
//code modified from: | |
//https://medium.com/tensorflow/a-gentle-introduction-to-tensorflow-js-dba2e5257702 | |
//define our inputs (combinations of 2 bits, represented as 0s and 1s) | |
//https://js.tensorflow.org/api/0.12.0/#tensor | |
var xs = tf.tensor([[0, 0], [0, 1], [1, 0], [1, 1]]); | |
//define our outputs (xor operation, a simple non-linear problem) |
This file contains hidden or 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
/** | |
original: the tensor to modify | |
axis: the axis to splice in | |
start: the position in the given axis to begin splicing from | |
deleteCount: how many frames to remove from the tensor | |
toInsert: the tensor to insert at "start" (all axes must match except for "axis") | |
*/ | |
function spliceTensor(original, axis, start, deleteCount, toInsert) | |
{ | |
var preStart = original.shape.map((e) => 0); |