0
import tensorflow as tf
import pandas as pd
import numpy as np
def normalize(data):
return data - np.min(data)/np.max(data) - np.min(data)
df = pd.read_csv('sat.csv', skipinitialspace=True)
x_reading = df['reading_score']
x_math = df['math_score']
x_reading, x_math = np.array(x_reading[df.reading_score != 's']), np.array(x_math[df.math_score != 's'])
x_data = normalize(np.float32(np.array([x_reading, x_math])))
y_writing = df[['writing_score']]
y_data = normalize(np.float32(np.array(y_writing[df.writing_score != 's'])))
W = tf.Variable(tf.random_uniform([1, 2], -.5, .5)) #float32
b = tf.Variable(tf.ones([1]))
y = tf.matmul(W, x_data) + b
loss = tf.reduce_mean(tf.square(y - y_data.T))
optimizer = tf.train.GradientDescentOptimizer(0.005)
train = optimizer.minimize(loss)
init = tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init)
for step in range(1000):
sess.run(train)
print step, sess.run(W), sess.run(b), sess.run(loss)
這是我的代碼。我的sat.csv包含SAT閱讀,寫作和數學成績的數據。你可以猜到,這些功能之間的差異並不大。My TensorFlow漸變下降
這是sat.csv的一部分。
DBN,SCHOOL NAME,Num of Test Takers,reading_score,math_score,writing_score
01M292,HENRY STREET SCHOOL FOR INTERNATIONAL STUDIES,29,355,404,363
01M448,UNIVERSITY NEIGHBORHOOD HIGH SCHOOL,91,383,423,366
01M450,EAST SIDE COMMUNITY SCHOOL,70,377,402,370
01M458,FORSYTH SATELLITE ACADEMY,7,414,401,359
01M509,MARTA VALLE HIGH SCHOOL,44,390,433,384
01M515,LOWER EAST SIDE PREPARATORY HIGH SCHOOL,112,332,557,316
01M539,"NEW EXPLORATIONS INTO SCIENCE, TECHNOLOGY AND MATH HIGH SCHOOL",159,522,574,525
01M650,CASCADES HIGH SCHOOL,18,417,418,411
01M696,BARD HIGH SCHOOL EARLY COLLEGE,130,624,604,628
02M047,47 THE AMERICAN SIGN LANGUAGE AND ENGLISH SECONDARY SCHOOL,16,395,400,387
我只使用數學,寫作和閱讀成績。我上面的代碼的目標是預測寫作分數,如果我給數學和閱讀分數。
我從未見過Tensorflow的梯度下降模型與這種簡單的數據不同。什麼錯了?
感謝您的幫助!但是,儘管我已經嘗試了您的建議,但似乎沒有任何改進。之前我沒有使用TF的線性迴歸代碼與上面的代碼有相同的問題。線性迴歸開始嚇到我了! –
哦,我剛剛將學習速率更改爲0.0000252,並且正在運行!非常感謝。但另一個問題是我的模型學得如此之慢。 –