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
| #%% gradient descent | |
| # *********************************************************** | |
| def run_gradient_descent(func, initial_point, gradients, verbose=False): | |
| """ | |
| Simple implementation of gradient descent for a convex function of 2 arguments. | |
| Arguments: | |
| func: the function of 2 variables to minimize | |
| initial_point: a 2D tuple representing the initial point |
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 React, { Component } from "react"; | |
| import Chart from "chart.js"; | |
| import "./App.css"; | |
| /* | |
| Mnist application | |
| Draw hand-written digits on a canvas and send it to backend server for classification. | |
| */ | |
| class App extends Component { | |
| constructor(props) { |
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 flask | |
| from flask_cors import CORS | |
| from flask_restplus import Api, Resource, fields | |
| import tensorflow as tf | |
| from skimage.transform import resize | |
| import numpy as np | |
| # disable GPU usage for inference | |
| import os | |
| os.environ["CUDA_VISIBLE_DEVICES"] = "-1" |
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
| # choose a loss function and an optimizer | |
| model.compile(loss=tf.keras.losses.categorical_crossentropy, | |
| optimizer=tf.keras.optimizers.Adam(), | |
| metrics=['accuracy']) | |
| # (optional) configure tensorboard to collect training stats | |
| log_dir = "C:\\temp\\tensorboard\\{}".format(datetime.now().strftime("%Y%m%d-%H%M%S")) | |
| os.mkdir(log_dir) | |
| tensorboardCallback = tf.keras.callbacks.TensorBoard(log_dir=log_dir) |
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 os | |
| from tensorflow.keras.models import Sequential | |
| from tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D | |
| from datetime import datetime | |
| # convert class vectors to binary class matrices | |
| num_classes = 10 | |
| y_train = tf.keras.utils.to_categorical(y_train, num_classes) | |
| y_test = tf.keras.utils.to_categorical(y_test, num_classes) |
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 | |
| print("Loading mnist dataset...") | |
| (X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data() | |
| X_train, X_test = X_train / 255.0, X_test / 255. | |
| # add a channels dimension | |
| X_train = X_train[..., tf.newaxis] | |
| X_test = X_test[..., tf.newaxis] | |
| print('Data loaded. X_train shape:', X_train.shape) |
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
| w = 10 | |
| h = 10 | |
| fig=plt.figure(figsize=(8, 8)) | |
| columns = 4 | |
| rows = 4 | |
| for i in range(0, columns*rows): | |
| img = X_train[i] | |
| ax = fig.add_subplot(rows, columns, i+1) | |
| ax.title.set_text('label:' + str(y_train[i])) | |
| plt.imshow(img) |
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 sympy | |
| x1 = sympy.symbols('x1') | |
| x2 = sympy.symbols('x2') | |
| x3 = sympy.symbols('x3') | |
| def f(x1, x2, x3): | |
| return 3*(x1**2 + x2*x3) | |
| u = f(x1, x2, x3) | |
| print('f(x1, x2, x3) = ',u) |