<jupyter_start><jupyter_text>Function Fitter<jupyter_code>f = 1
X_train = rand.uniform(-1, 1, 500)
X_train = np.sort(X_train)
y_train = np.sin(2*np.pi*f*X_train)

X_train = np.expand_dims(X_train, 1)
y_train = np.expand_dims(y_train, 1)

X_test = np.expand_dims(np.linspace(-1, 1, 500), 1)
model = models.Sequential()
model.add(layers.InputLayer(input_shape=(1,),
                            name='input'))
for i, u in enumerate([50]):
    model.add(layers.Dense(units=u,
                           kernel_initializer='random_uniform',
                           activation='relu',
                           name='hidden%s' %str(i+1)))
model.add(layers.Dense(units=1,
                       activation='linear',
                       name='output'))
model.summary()
epochs = int(1e6)
batch_size = len(X_train)
stopval = StopOnValue(monitor='loss',
                      mode='min',
                      value=0.01)
lrred = callbacks.ReduceLROnPlateau(min_lr=1e-4,
                                    patience=10,
                                    factor=0.2,
                                    mode='min')
estop = callbacks.EarlyStopping(monitor='loss',
                                min_delta=1e-2,
                                patience=10,
                                verbose=0,
                                mode='min',
                                baseline=0.1)
sgdr = SGDRScheduler(min_lr=1e-4,
                     max_lr=1e-2,
                     steps_per_epoch=np.ceil(epochs/batch_size),
                     lr_decay=0.9,
                     cycle_length=5,
                     mult_factor=1.5)
checkpoint = callbacks.ModelCheckpoint('funcfit_best_weights.h5',
                                       monitor='loss',
                                       verbose=0,
                                       save_best_only=True,
                                       save_weights_only=True,
                                       mode='min')

model.compile(loss=l2loss,
              optimizer=AdaBound(lr=0.1,
                                 final_lr=1e-3,
                                 amsbound=True))
history = model.fit(X_train, y_train,
                    epochs=epochs,
                    batch_size=batch_size,
                    verbose=0,
                    callbacks=[stopval, checkpoint])
plt.plot(history.epoch, history.history['loss'])
plt.xlabel('epoch')
plt.ylabel('loss')
# plt.savefig('fit-2-loss.png', bbox_inches='tight', dpi=300)
plt.show()

res = model.predict(X_test).squeeze()

plt.plot(X_train, y_train, '.', label='ground truth')
plt.plot(X_test, res, label='predicted')
plt.legend()
# plt.savefig('fit-2-pred.png', bbox_inches='tight', dpi=300)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12068338785896038, 'fraction_numerical': 0.022900763358778626, 'mean_word_length': 1.4527629233511588, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '49285', 'license': 'MIT', 'n_tokens_mistral': 886, 'n_tokens_neox': 831, 'n_words': 108, 'path': 'Act_17.ipynb', 'repo_name': 'marychrisgo/Image-Processing-Activities', 'size': '2751'}