def test_forward(self): x = ad.placeholder(shape=(2, 3)) feed_dict = {x: np.random.random((2, 3))} actual = ad.Session().run(x, feed_dict=feed_dict) expect = feed_dict[x] self.assertTrue(np.allclose(expect, actual), (expect, actual)) feed_dict = {x: np.random.random((2, 3))} actual = ad.Session().run(x, feed_dict=feed_dict) expect = feed_dict[x] self.assertTrue(np.allclose(expect, actual), (expect, actual))
def train_model(loss: ad.Operation, placeholders: list, variables: list, config: dict, verbose=False) -> None: """Train the linear model with SGD. :param loss: Loss operation. :param placeholders: Placeholders of inputs. :param variables: Trainable variables. :param config: Configuration. :param verbose: Whether to show the losses. """ sess = ad.Session() x, y = placeholders learning_rate = config['learning_rate'] for step, (batch_x, batch_y) in enumerate(data_generator(config)): if step > 50000: break sess.prepare() feed_dict = {x: batch_x, y: batch_y} loss_val = sess.run(loss, feed_dict=feed_dict) loss.backward() lr = learning_rate / (1.0 + 1e-5 * step) for var in variables: var.update_add(-lr * var.gradient) if verbose: print('\rStep %d - Loss %.4f' % (step, loss_val), end='') if loss_val < 1e-4: break if verbose: print('')
def check_result(model: ad.Operation, placeholders: list, config: dict, verbose=False): """Check the trained model. :param model: The linear model. :param placeholders: Placeholders of inputs. :param config: Configuration. :param verbose: Whether to show the sample outputs. """ sess = ad.Session() x, y_true = placeholders for batch_x, batch_y in data_generator(config): sess.prepare() feed_dict = {x: batch_x, y_true: batch_y} y_pred_val = sess.run(model, feed_dict=feed_dict) y_true_cls = np.argmax(batch_y, axis=-1) y_pred_cls = np.argmax(y_pred_val, axis=-1) accuracy = (y_true_cls == y_pred_cls).astype( np.float64).sum() / config['batch_size'] if verbose: print('Expected: ', y_true_cls[:5]) print('Actual: ', y_pred_cls[:5]) print('Accuracy: ', accuracy) assert accuracy > 0.8 break
def test_forward(self): sess = ad.Session() val = np.random.random((2, 3)) w = ad.variable(val) actual = sess.run(w) expect = val self.assertTrue(np.allclose(expect, actual), (expect, actual)) sess.prepare() val = np.random.random((2, 3)) w.update(val) actual = sess.run(w) expect = val self.assertTrue(np.allclose(expect, actual), (expect, actual))
def check_result(model: ad.Operation, placeholders: list, config: dict, verbose=False): """Check the trained model. :param model: The linear model. :param placeholders: Placeholders of inputs. :param config: Configuration. :param verbose: Whether to show the sample outputs. """ sess = ad.Session() x, y = placeholders for batch_x, batch_y in data_generator(config): sess.prepare() feed_dict = {x: batch_x, y: batch_y} y_pred_val = sess.run(model, feed_dict=feed_dict) if verbose: print('Expected: ', batch_y[:5]) print('Actual: ', y_pred_val[:5]) assert np.alltrue(batch_y - y_pred_val < 1.0) break
def test_twice(self): sess = ad.Session() op = ad.constant(np.array(1.0)) sess.run(op) sess.run(op)