Skip to content

Instantly share code, notes, and snippets.

@justinhou95
Last active March 1, 2023 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinhou95/11d922d9f8c50fce038271bd01b86393 to your computer and use it in GitHub Desktop.
Save justinhou95/11d922d9f8c50fce038271bd01b86393 to your computer and use it in GitHub Desktop.
#Solution to Q1:
def construct_func_nn(x_grid,y_grid,width,f2):
def func_nn(x):
y = 0
for i in range(len(x_grid)):
x0 = x_grid[i]
y0 = y_grid[i]
y += f2((x-x0)/width)*y0
return y
return func_nn
#Solution to Q2:
def construct_hat2d(r,f2):
func1 = lambda x,y: f2(x)
func2 = lambda x,y: f2(y)
func3 = lambda x,y: f2(x) + f2(y)
func4 = lambda x,y: r(f2(x) + f2(y) - 1 )
funcs = [func1, func2, func3, func4]
return funcs
#Solution to Q3:
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
def construct_nn():
layer1 = layers.Dense(10, activation="relu", name="layer1")
layer2 = layers.Dense(10, activation="relu", name="layer2")
layer3 = layers.Dense(1, name="layer3")
model = keras.Sequential([layer1, layer2, layer3])
model.build((None,2))
return model
def construct_Vnn(func_nn):
inputs = keras.Input(shape=(1,))
S = inputs
for i in range(10):
t = tf.ones(tf.shape(inputs)) * i
S_and_t = tf.concat([S,t],axis = -1)
S = S + func_nn(S_and_t)
outputs = S
model = keras.Model(inputs=inputs, outputs=outputs)
return model
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment