Last active
December 3, 2017 00:19
-
-
Save yongjun823/5a5c14d7deed2c0a3793ef0464318a86 to your computer and use it in GitHub Desktop.
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 pandas as pd | |
| import tensorflow as tf | |
| train = pd.read_csv("data_bike/train.csv", parse_dates=["datetime"]) | |
| test = train[:2000] | |
| train= train[2000:] | |
| feature=['temp', 'atemp', 'humidity', 'windspeed', 'casual', 'registered'] | |
| x_data = train[feature] | |
| y_data = train[['count']] | |
| x_data = np.array(x_data, dtype=np.float32).reshape((-1, 2, 3, 1)) | |
| y_data = np.array(y_data, dtype=np.int32) | |
| X = tf.placeholder(tf.float32, [None, 2, 3, 1]) | |
| Y = tf.placeholder(tf.int32, [None, 1]) | |
| conv1 = tf.layers.conv2d(inputs=X, filters=10, kernel_size=[2, 2], | |
| padding="same", activation=tf.nn.relu) | |
| conv2 = tf.layers.conv2d(inputs=conv1, filters=15, kernel_size=[2, 2], | |
| padding="same", activation=tf.nn.relu) | |
| conv3 = tf.layers.conv2d(inputs=conv2, filters=20, kernel_size=[3, 3], | |
| padding="same") | |
| pool1 = tf.layers.max_pooling2d(inputs=conv3, pool_size=[2, 2], strides=1) | |
| flatten = tf.contrib.layers.flatten(pool1) | |
| fc1 = tf.layers.dense(inputs=flatten, units=30, activation=tf.nn.relu) | |
| fc2 = tf.layers.dense(inputs=fc1, units=20, activation=tf.nn.relu) | |
| fc3 = tf.layers.dense(inputs=fc2, units=10, activation=tf.nn.relu) | |
| fc4 = tf.layers.dense(inputs=fc3, units=1) | |
| loss = tf.losses.mean_squared_error(Y, fc4) | |
| tf.summary.scalar('loss', loss) | |
| optimizer = tf.train.AdamOptimizer().minimize(loss) | |
| init = tf.global_variables_initializer() | |
| summary = tf.summary.merge_all() | |
| sess = tf.Session() | |
| sess.run(init) | |
| merged = tf.summary.merge_all() | |
| train_writer = tf.summary.FileWriter('ttrain', sess.graph) | |
| for idx in range(10000): | |
| s, c, _= sess.run([summary, loss, optimizer], feed_dict={ | |
| X: x_data, | |
| Y: y_data | |
| }) | |
| train_writer.add_summary(s, global_step=idx) | |
| if (idx+1) % 100 == 0: | |
| print(idx, c) | |
| feature=['temp', 'atemp', 'humidity', 'windspeed', 'casual', 'registered'] | |
| x_test = test[feature] | |
| x_test = np.array(x_test).reshape((-1, 2, 3, 1)) | |
| y_test = test[['count']] | |
| y_tesst = np.array(y_test) | |
| print('test predict') | |
| accuracy = sess.run(loss, feed_dict={ | |
| X: x_test, | |
| Y: y_test | |
| }) | |
| print(1 - accuracy) |
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
| # https://www.kaggle.com/c/bike-sharing-demand/data | |
| import numpy as np | |
| import pandas as pd | |
| import tensorflow as tf | |
| train = pd.read_csv("data_bike/train.csv", parse_dates=["datetime"]) | |
| test = pd.read_csv("data_bike/test.csv", parse_dates=["datetime"]) | |
| label_name = "count" | |
| y_data = train[['count']] | |
| print(y_data.shape) | |
| feature=['temp', 'atemp', 'humidity', 'windspeed', 'casual', 'registered'] | |
| x_data = train[feature] | |
| x_data.head() | |
| X = tf.placeholder(tf.float32, [None, 6]) | |
| Y = tf.placeholder(tf.int32, [None, 1]) | |
| layer1 = tf.layers.dense(inputs=X, units=100, activation=tf.nn.relu) | |
| layer2 =tf.layers.dense(inputs=layer1, units=90, activation=tf.nn.relu) | |
| layer3 =tf.layers.dense(inputs=layer2, units=50, activation=tf.nn.relu) | |
| layer4 =tf.layers.dense(inputs=layer3, units=30, activation=tf.nn.relu) | |
| layer5 =tf.layers.dense(inputs=layer4, units=1) | |
| cost = tf.losses.mean_squared_error(Y, layer5) | |
| tf.summary.scalar('cost', cost) | |
| train = tf.train.AdamOptimizer().minimize(cost) | |
| init = tf.global_variables_initializer() | |
| summary = tf.summary.merge_all() | |
| with tf.Session() as sess: | |
| sess.run(init) | |
| merged = tf.summary.merge_all() | |
| train_writer = tf.summary.FileWriter('train', sess.graph) | |
| for idx in range(10000): | |
| s, c, _= sess.run([summary, cost, train], feed_dict={ | |
| X: x_data, | |
| Y: y_data | |
| }) | |
| train_writer.add_summary(s, global_step=idx) | |
| if idx % 1000 == 0: | |
| print(idx, c) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment