{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%load_ext autoreload\n", "%autoreload 2\n", "\n", "import tensorflow as tf\n", "import gradio" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "n_classes = 10\n", "(x_train, y_train),(x_test, y_test) = tf.keras.datasets.mnist.load_data()\n", "x_train, x_test = x_train.reshape(-1, 784) / 255.0, x_test.reshape(-1, 784) / 255.0\n", "y_train = tf.keras.utils.to_categorical(y_train, n_classes).astype(float)\n", "y_test = tf.keras.utils.to_categorical(y_test, n_classes).astype(float)\n", "\n", "learning_rate = 0.5\n", "epochs = 5\n", "batch_size = 100" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "WARNING:tensorflow:From C:\\Users\\ALI\\Anaconda3\\lib\\site-packages\\tensorflow\\python\\framework\\op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.\n", "Instructions for updating:\n", "Colocations handled automatically by placer.\n" ] } ], "source": [ "x = tf.placeholder(tf.float32, [None, 784], name=\"x\")\n", "y = tf.placeholder(tf.float32, [None, 10], name=\"y\")\n", "\n", "W1 = tf.Variable(tf.random_normal([784, 300], stddev=0.03), name='W1')\n", "b1 = tf.Variable(tf.random_normal([300]), name='b1')\n", "W2 = tf.Variable(tf.random_normal([300, 10], stddev=0.03), name='W2')\n", "hidden_out = tf.add(tf.matmul(x, W1), b1)\n", "hidden_out = tf.nn.relu(hidden_out)\n", "y_ = tf.matmul(hidden_out, W2)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "probs = tf.nn.softmax(y_)\n", "cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=y_, labels=y))\n", "optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cross_entropy)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "init_op = tf.global_variables_initializer()\n", "correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))\n", "accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "sess = tf.Session()\n", "sess.run(init_op)\n", "total_batch = int(len(y_train) / batch_size)\n", "for epoch in range(epochs):\n", " avg_cost = 0\n", " for start, end in zip(range(0, len(y_train), batch_size), range(batch_size, len(y_train)+1, batch_size)): \n", " batch_x = x_train[start: end]\n", " batch_y = y_train[start: end]\n", " _, c = sess.run([optimizer, cross_entropy], feed_dict={x: batch_x, y: batch_y})\n", " avg_cost += c / total_batch" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "def predict(inp):\n", " return sess.run(probs, feed_dict={x:inp})" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "inp = gradio.inputs.Sketchpad(flatten=True)\n", "io = gradio.Interface(inputs=inp, outputs=\"label\", model_type=\"pyfunc\", model=predict)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "No validation samples for this interface... skipping validation.\n", "NOTE: Gradio is in beta stage, please report all bugs to: a12d@stanford.edu\n", "Model is running locally at: http://localhost:7860/interface.html\n", "Model available publicly for 8 hours at: https://share.gradio.app/25d5d472\n" ] }, { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "(.HTTPServer at 0x229f97553c8>,\n", " 'http://localhost:7860/',\n", " 'http://25d5d472.ngrok.io')" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "io.launch(share=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.1" } }, "nbformat": 4, "nbformat_minor": 2 }