0
我想在張量流中訓練一次模型,然後想要使用訓練好的模型來預測一些函數。在我們進入細節,讓我們定義一對夫婦的功能...張量流計算不需要再培訓
def runTF(func, inpDict):
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
result = sess.run(func, feed_dict = inpDict)
sess.close()
return result
和
def optTF(opt, res, others, inpDict, nSteps, printSteps=50):
os, re = [], []
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
for i in range(nSteps):
# First run the optimizer ...
sess.run(opt, feed_dict = inpDict)
# Save all the data you want to save
temp = sess.run([res] + others, feed_dict = inpDict)
re.append(temp[0])
os.append(temp[1:])
if (i%printSteps) == 0:
print('{:5d}'.format(i))
sess.close()
return re, os
下面是我在做什麼幾個步驟...
A.生成一些數據
N = 500
features = 2
nSteps = 1000
X = np.array([np.random.random(N), np.random.random(N)])
data = [X.T, X[0].reshape(-1, 1)]
B.創建一個簡單的線性模型
d = tf.placeholder(shape = np.shape(data[0]), dtype = tf.float32, name='d') # input layer
dOut = tf.placeholder(shape = np.shape(data[1]), dtype = tf.float32, name='dOut') # output layer
W = tf.Variable( np.random.randn(features, 1), dtype = tf.float32, name='W')
b = tf.Variable( np.zeros((1,1)), dtype = tf.float32, name='b')
result = tf.matmul(d, W)+b
cost = tf.reduce_mean((dOut - result)**2)
optimizer = tf.train.AdamOptimizer(learning_rate = 0.1).minimize(cost)
C.讓我們運行優化器
inpDict = {d: data[0], dOut:data[1]}
ot = optTF(optimizer, result, [W, cost], inpDict, 200, 50)
在這裏,我已經檢查的結果,並認爲它就是我想要的。所以優化器工作正常。該模型已經優化。現在,我想用其他一些數據進行預測。所以我做...
r = runTF(result, inpDict)
這一新的結果是不是我會從相同的訓練模型的期望。
現在,只要我呆在同一個tf.Session()
,我們沒事。但是,即使在完成會話時,我也希望能夠做出預測。所以我的問題是,一旦我們在不同的會話中的一個會話中訓練它,我該如何使用模型?
請注意,整個事情是我在不同的會話中做的事情?
編輯:
我編輯了兩個函數以納入節能...
def runTF(func, inpDict, modelFile=None):
if modelFile is not None:
saver = tf.train.Saver()
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
if modelFile is not None:
ckpt = tf.train.get_checkpoint_state(modelFile)
print(modelFile, ckpt)
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, ckpt.model_checkpoint_path)
print('Session restored')
result = sess.run(func, feed_dict = inpDict)
sess.close()
return result
和
def optTF(opt, res, others, inpDict, nSteps, printSteps=50, modelFile='models/temp.ckpt'):
os, re = [], []
saver = tf.train.Saver()
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
for i in range(nSteps):
# First run the optimizer ...
sess.run(opt, feed_dict = inpDict)
# Save all the data you want to save
temp = sess.run([res] + others, feed_dict = inpDict)
re.append(temp[0])
os.append(temp[1:])
if (i%printSteps) == 0:
print('{:5d}'.format(i))
path = saver.save(sess, modelFile)
print('Model saved in: {}'.format(path))
sess.close()
return re, os
而且運行模型爲:
ot = optTF(optimizer, result, [cost]+weights+biases, inpDict, 200, 50)
r = runTF([result], inpDict, 'models/temp.ckpt')
什麼都沒有!我檢查了:
- 的
ckpt
值是None
- 的模型文件夾中有下列文件:
- 關卡
- temp.ckpt.index
- temp.ckpt.data- 00000-of-00001
- temp.ckpt.meta
謝謝@andresbravog。我確實試圖挽救像你所說的狀態。我編輯了我的答案。請參閱上文 – ssm