Skip to content

Commit

Permalink
Update for chapter 3 and TF v1.7
Browse files Browse the repository at this point in the history
  • Loading branch information
nfmcclure committed Apr 1, 2018
1 parent 0234ac9 commit 7dd2714
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
from tensorflow.python.framework import ops
ops.reset_default_graph()

# Set a random seed
tf.set_random_seed(42)
np.random.seed(42)

# Create graph
sess = tf.Session()

Expand Down Expand Up @@ -45,7 +49,7 @@
loss = tf.reduce_mean(tf.truediv(demming_numerator, demming_denominator))

# Declare optimizer
my_opt = tf.train.GradientDescentOptimizer(0.1)
my_opt = tf.train.GradientDescentOptimizer(0.15)
train_step = my_opt.minimize(loss)

# Initialize variables
Expand All @@ -54,14 +58,14 @@

# Training loop
loss_vec = []
for i in range(250):
for i in range(1000):
rand_index = np.random.choice(len(x_vals), size=batch_size)
rand_x = np.transpose([x_vals[rand_index]])
rand_y = np.transpose([y_vals[rand_index]])
sess.run(train_step, feed_dict={x_data: rand_x, y_target: rand_y})
temp_loss = sess.run(loss, feed_dict={x_data: rand_x, y_target: rand_y})
loss_vec.append(temp_loss)
if (i+1)%50==0:
if (i+1)%50 == 0:
print('Step #' + str(i+1) + ' A = ' + str(sess.run(A)) + ' b = ' + str(sess.run(b)))
print('Loss = ' + str(temp_loss))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@
},
"outputs": [],
"source": [
"sess = tf.Session()"
"sess = tf.Session()\n",
"\n",
"# Set a random seed\n",
"tf.set_random_seed(42)\n",
"np.random.seed(42)"
]
},
{
Expand Down Expand Up @@ -365,7 +369,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
"version": "3.6.1"
}
},
"nbformat": 4,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@


# Specify 'Ridge' or 'LASSO'
regression_type = 'LASSO'
#regression_type = 'LASSO'
regression_type = 'Ridge'

# clear out old graph
ops.reset_default_graph()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
" birth_data = birth_file.text.split('\\r\\n')\n",
" birth_header = birth_data[0].split('\\t')\n",
" birth_data = [[float(x) for x in y.split('\\t') if len(x)>=1] for y in birth_data[1:] if len(y)>=1]\n",
" with open(birth_weight_file, \"w\") as f:\n",
" with open(birth_weight_file, 'w', newline='') as f:\n",
" writer = csv.writer(f)\n",
" writer.writerow(birth_header)\n",
" writer.writerows(birth_data)\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,23 @@
# Obtain and prepare data for modeling
###

# name of data file
# Set name of data file
birth_weight_file = 'birth_weight.csv'

# download data and create data file if file does not exist in current directory
# Download data and create data file if file does not exist in current directory
if not os.path.exists(birth_weight_file):
birthdata_url = 'https://github.com/nfmcclure/tensorflow_cookbook/raw/master/01_Introduction/07_Working_with_Data_Sources/birthweight_data/birthweight.dat'
birth_file = requests.get(birthdata_url)
birth_data = birth_file.text.split('\r\n')
birth_header = birth_data[0].split('\t')
birth_data = [[float(x) for x in y.split('\t') if len(x)>=1] for y in birth_data[1:] if len(y)>=1]
with open(birth_weight_file, "w") as f:
with open(birth_weight_file, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(birth_header)
writer.writerows(birth_data)
f.close()

# read birth weight data into memory
# Read birth weight data into memory
birth_data = []
with open(birth_weight_file, newline='') as csvfile:
csv_reader = csv.reader(csvfile)
Expand All @@ -58,7 +58,7 @@
# Pull out predictor variables (not id, not target, and not birthweight)
x_vals = np.array([x[1:8] for x in birth_data])

# set for reproducible results
# Set for reproducible results
seed = 99
np.random.seed(seed)
tf.set_random_seed(seed)
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tensorflow==1.6.0
tensorflow==1.7.0
numpy==1.14.2
scipy==1.0.0
sklearn==0.19.1
Expand Down

0 comments on commit 7dd2714

Please sign in to comment.