Skip to content

Commit

Permalink
tensorflow multi layers
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmadhajmosa committed Jun 5, 2019
1 parent c25a070 commit a31a49a
Showing 1 changed file with 146 additions and 26 deletions.
172 changes: 146 additions & 26 deletions Session_2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,7 @@
"metadata": {
"id": "hlmSCbhtoJBs",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 550
},
"outputId": "0b9a7b9e-a313-43f9-c133-b2ca0f30d56f"
"colab": {}
},
"source": [
"import tensorflow as tf\n",
Expand Down Expand Up @@ -204,6 +200,7 @@
"\n",
"# Launch the graph\n",
"with tf.Session() as sess:\n",
" \n",
" sess.run(init)\n",
" \n",
" for i in range(10):\n",
Expand All @@ -220,31 +217,64 @@
" print('real ', y_gr)\n",
"\n"
],
"execution_count": 7,
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "fv2aqi3Fu-AJ",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 550
},
"outputId": "eabf27bc-af78-4605-b30d-27f8077196c3"
},
"source": [
"sess = tf.Session() \n",
"sess.run(init)\n",
" \n",
"for i in range(10):\n",
" \n",
" sess.run(optimizer, feed_dict={x: x_gr, y: y_gr}) \n",
"\n",
" pr_cost = sess.run(cost, feed_dict={x: x_gr,y: y_gr})\n",
" \n",
" print('iter: ',i, 'cost: ', pr_cost)\n",
" \n",
"y_p_p = sess.run(y_p, feed_dict={x: x_gr, y: y_gr})\n",
" \n",
"print('predicted ', y_p_p)\n",
"print('real ', y_gr)\n",
"\n",
"#sess.close()"
],
"execution_count": 10,
"outputs": [
{
"output_type": "stream",
"text": [
"iter: 0 cost: 0.6431273\n",
"iter: 1 cost: 0.6418297\n",
"iter: 2 cost: 0.64053476\n",
"iter: 3 cost: 0.63924253\n",
"iter: 4 cost: 0.6379529\n",
"iter: 5 cost: 0.6366661\n",
"iter: 6 cost: 0.6353822\n",
"iter: 7 cost: 0.6341011\n",
"iter: 8 cost: 0.6328229\n",
"iter: 9 cost: 0.63154775\n",
"predicted [[ 0.6446512 -0.20713729 2.1476808 -1.4118636 ]\n",
" [-0.12064859 0.07385027 0.5683408 0.07175648]\n",
" [ 0.2990489 -0.06490395 1.4139478 -0.84314406]\n",
" [ 0.62124217 -0.35937288 0.48357993 -0.37525392]\n",
" [ 0.16393916 -0.1274146 0.4014422 0.12488011]\n",
" [ 0.55494636 -0.29376802 0.8285301 -0.49846467]\n",
" [ 0.3764549 -0.13861422 1.3277904 -0.7060356 ]\n",
" [ 0.5982744 -0.45127493 -0.2668337 0.29937968]\n",
" [-0.00946417 0.01262925 0.5629873 -0.01921529]\n",
" [-0.13898507 0.05959349 0.37287554 0.23871103]]\n",
"iter: 0 cost: 0.5914289\n",
"iter: 1 cost: 0.590312\n",
"iter: 2 cost: 0.5891979\n",
"iter: 3 cost: 0.5880863\n",
"iter: 4 cost: 0.5869776\n",
"iter: 5 cost: 0.58587164\n",
"iter: 6 cost: 0.58476853\n",
"iter: 7 cost: 0.5836683\n",
"iter: 8 cost: 0.5825709\n",
"iter: 9 cost: 0.5814764\n",
"predicted [[-0.07052338 -1.4182088 -0.20726368 0.6065073 ]\n",
" [-0.41022846 0.7640786 1.0915788 0.5919728 ]\n",
" [-0.07066503 -0.73823565 0.10052644 0.31935224]\n",
" [-0.2946304 -0.3036949 -0.28762203 1.1227872 ]\n",
" [-0.57840925 0.8397043 0.8986697 1.1970941 ]\n",
" [-0.32035732 -0.3043637 -0.03262401 1.0779576 ]\n",
" [-0.25558242 -0.41777548 0.2828037 0.74326414]\n",
" [-0.6702078 0.774465 0.18325244 1.8486446 ]\n",
" [-0.3652569 0.5447639 0.8380182 0.6200657 ]\n",
" [-0.48676854 1.0157775 1.195501 0.7295743 ]]\n",
"real [[0.28427788 0.58920269 0.76702838 0.54495026]\n",
" [0.10077242 0.44421162 0.08474814 0.63117015]\n",
" [0.01157148 0.14526119 0.81988115 0.22006669]\n",
Expand All @@ -259,6 +289,96 @@
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "vUPSS03avw5D",
"colab_type": "code",
"colab": {}
},
"source": [
"import tensorflow as tf\n",
"import numpy as np\n",
"\n",
"# Parameters\n",
"learning_rate = 0.001\n",
"training_iters = 2000\n",
"batch_size = 128\n",
"\n",
"# Network Parameters\n",
"\n",
"num_inputs = 3\n",
"num_h1_n = 4\n",
"num_h2_n = 10\n",
"num_outputs = 4\n",
"\n",
"num_samples= 10\n",
"\n",
"# Training data\n",
"x_gr = np.random.rand(num_samples,num_inputs)\n",
"y_gr = np.random.rand(num_samples,num_outputs)\n",
"\n",
"\n",
"# tf Graph input\n",
"x = tf.placeholder(tf.float32, [None, num_inputs])\n",
"y = tf.placeholder(tf.float32, [None, num_outputs])\n",
"\n",
"\n",
"# weights \n",
"w_1 = tf.Variable(tf.random_normal([num_inputs,num_h1_n ]))\n",
"w_2 = tf.Variable(tf.random_normal([num_h1_n,num_h2_n ]))\n",
"w_3 = tf.Variable(tf.random_normal([num_h2_n,num_outputs ]))\n",
"\n",
"# bias \n",
"b_1 = tf.Variable(tf.random_normal([num_h1_n]))\n",
"b_2 = tf.Variable(tf.random_normal([num_h2_n]))\n",
"b_3 = tf.Variable(tf.random_normal([num_outputs]))\n",
"\n",
"\n",
"\n",
"\n",
"# model\n",
"\n",
"h1 = tf.nn.sigmoid(tf.add(tf.matmul(x, w_1),b_1)) # model of hidden layer 1\n",
"h2 = tf.nn.sigmoid(tf.add(tf.matmul(h1, w_2),b_2)) # model of hidden layer 2\n",
"y_p = tf.add(tf.matmul(h2, w_3),b_3) # model of the output layer\n",
"\n",
"\n",
"\n",
"# cost\n",
"\n",
"cost = tf.reduce_mean(tf.pow(y-y_p,2)) # \n",
"\n",
"# optimisation \n",
"\n",
"optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)\n",
"\n",
"# initalizing the graph and the weights\n",
"\n",
"init = tf.global_variables_initializer()\n",
"\n",
"# Launch the graph\n",
"with tf.Session() as sess:\n",
" \n",
" sess.run(init)\n",
" \n",
" for i in range(10):\n",
" \n",
" sess.run(optimizer, feed_dict={x: x_gr, y: y_gr}) \n",
"\n",
" pr_cost = sess.run(cost, feed_dict={x: x_gr,y: y_gr})\n",
" \n",
" print('iter: ',i, 'cost: ', pr_cost)\n",
" \n",
" y_p_p = sess.run(y_p, feed_dict={x: x_gr, y: y_gr})\n",
" \n",
" print('predicted ', y_p_p)\n",
" print('real ', y_gr)\n",
"\n"
],
"execution_count": 0,
"outputs": []
}
]
}

0 comments on commit a31a49a

Please sign in to comment.