2017-07-25 60 views
0

我一直在關注本教程,以瞭解線性分類模型及其應用。我在人口普查數據之外採取了一個不同的例子,我可以通過evaluate獲得準確性。如何編寫張量流中的數據輸出

現在我感興趣的是用預測列值打印出測試數據行

https://www.tensorflow.org/tutorials/wide

import random 
import pandas 
import tensorflow as tf 
import tempfile 
import numpy as np 

df_train = pandas.read_csv('input/train.csv', usecols=['Sex', 'Age', 'Fare','Survived', 'SibSp']) 
df_test = pandas.read_csv('input/test.csv', usecols=['Sex', 'Age', 'Fare', 'SibSp']) 
df_test['Survived'] = 0 
CATEGORICAL_COLUMNS = ['Sex'] 
CONTINUOUS_COLUMNS = ['Age', 'Fare', 'SibSp'] 
df_train_nona = df_train.dropna() 
df_test_nona = df_test.dropna() 
print(df_test_nona) 
def input_fn(df): 
    continuous_cols = {k: tf.constant(df[k].values) 
         for k in CONTINUOUS_COLUMNS} 

    categorical_cols = {k: tf.SparseTensor(
     indices=[[i,0] for i in range(df[k].size)], 
     values=df[k].values, 
     dense_shape=[df[k].size, 1]) 
     for k in CATEGORICAL_COLUMNS 
    } 

    feature_cols = dict(list(continuous_cols.items()) + list(categorical_cols.items())) 

    label = tf.constant(df['Survived'].values) 

    return feature_cols, label 

def train_input_fn(): 
    return input_fn(df_train_nona) 

def eval_input_fn(): 
    return input_fn(df_test_nona) 

gender = tf.contrib.layers.sparse_column_with_keys(
    column_name='Sex', keys=['female', 'male'] 
) 

pclass = tf.contrib.layers.real_valued_column('Pclass') 

cabin = tf.contrib.layers.sparse_column_with_hash_bucket("Cabin", hash_bucket_size=1000) 

age = tf.contrib.layers.real_valued_column('Age') 
fare = tf.contrib.layers.real_valued_column('Fare') 
parch = tf.contrib.layers.real_valued_column('Parch') 
sibsp = tf.contrib.layers.real_valued_column('SibSp') 

model_dir = tempfile.mkdtemp() 

m = tf.contrib.learn.LinearClassifier(feature_columns=[gender, age, fare, sibsp], optimizer=tf.train.FtrlOptimizer(
     learning_rate=0.1, 
     l1_regularization_strength=0.001 
    ),model_dir=model_dir) 

m.fit(input_fn = train_input_fn, steps=400) 
+0

這是使用估算?他們有一個'predict()'方法,聽起來像你想要的。 –

+0

@AllenLavoie我可以使用'predict()'方法來獲取我預測的列。我只通過迭代生成器來接收一列。它是否保留了我傳遞給估計器的測試行的順序?我添加了我的示例代碼(不要嘲笑silliness :)我對張量流和ML非常陌生) –

+0

是的,'predict()'只會遍歷輸出的批量維度。它看起來不像輸入函數中有任何混洗,所以順序應該與輸入相同。 –

回答

0

做到這一點,最好的辦法是隻取輸入數據,並通過網絡功能運行英寸我相信這應該是sess.run(output_tensor, feed_dict={x: input_data})

+0

'input_data'是否與我的描述中的輸入函數相對應? –

0

比方說,你創建兩個浮點

node1 = tf.constant(3.0, dtype=tf.float32) 
node2 = tf.constant(4.0) # also tf.float32 implicitly 
print(node1, node2) 

上面的代碼將打印此

Tensor("Const:0", shape=(), dtype=float32) Tensor("Const_1:0", shape=(), dtype=float32) 

但你可能不希望它被印像。嘗試使用sess = tf.Session()

sess = tf.Session() 
print(sess.run([node1, node2])) 

將印像這樣

[3.0, 4.0] 

希望它能幫助:)

+0

這將與我上面的示例一起工作嗎?不知道如何使用這種方法與我有蹩腳的代碼:) –

+0

這將與我的例子上面的工作?不知道如何使用這種方法與我有蹩腳的代碼:) –

相關問題