Skip to content

Commit b2067ca

Browse files
committed
Added file reading
1 parent 4fa0b46 commit b2067ca

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Lab 4 Multi-variable linear regression
2+
import tensorflow as tf
3+
import numpy as np
4+
5+
xy = np.loadtxt('data.csv', delimiter=',', dtype=np.float32)
6+
x_data = xy[:, 0:-1]
7+
y_data = xy[:, [-1]]
8+
9+
print(x_data.shape, x_data, len(x_data))
10+
print(y_data.shape, y_data)
11+
12+
W = tf.Variable(tf.random_uniform(
13+
shape=[3, 1], minval=-1.0, maxval=1.0, dtype=tf.float32))
14+
b = tf.Variable(tf.random_uniform(
15+
shape=[1], minval=-1.0, maxval=1.0, dtype=tf.float32))
16+
17+
# Hypothesis
18+
hypothesis = tf.matmul(x_data, W) + b
19+
20+
# Simplified cost function
21+
cost = tf.reduce_mean(tf.square(hypothesis - y_data))
22+
23+
# Minimize
24+
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
25+
train = optimizer.minimize(cost)
26+
27+
# Initialize variables
28+
init = tf.global_variables_initializer()
29+
30+
# Launch graph
31+
sess = tf.Session()
32+
sess.run(init)
33+
34+
for step in range(2001):
35+
sess.run(train)
36+
if step % 20 == 0:
37+
print(step, sess.run(cost),
38+
sess.run(hypothesis), sess.run(W), sess.run(b))
File renamed without changes.

0 commit comments

Comments
 (0)