如何在tensorflow模型内打印转换后的特征值

如何在tensorflow模型内打印转换后的特征值,tensorflow,machine-learning,data-science,tensorboard,Tensorflow,Machine Learning,Data Science,Tensorboard,如何才能看到tensorflow模型中正在训练的最终特征的价值。就像在下面的例子中,我正在尝试对我的专栏“x”进行多点热处理,我想看看这些功能是如何应用到我的模型中的 这在sklearn中很容易做到,但作为Tensorflow的新手,我不明白这怎么可能 import tensorflow as tf import pandas as pd data = {'x':['a c', 'a b', 'b c'], 'y': [1, 1, 0]} df = pd.DataFrame(data) Y

如何才能看到tensorflow模型中正在训练的最终特征的价值。就像在下面的例子中,我正在尝试对我的专栏“x”进行多点热处理,我想看看这些功能是如何应用到我的模型中的

这在sklearn中很容易做到,但作为Tensorflow的新手,我不明白这怎么可能

import tensorflow as tf
import pandas as pd

data = {'x':['a c', 'a b', 'b c'], 'y': [1, 1, 0]}

df = pd.DataFrame(data)
Y = df['y']
X = df.drop('y', axis=1)
indicator_features = [tf.feature_column.indicator_column(categorical_column=
      tf.feature_column.categorical_column_with_vocabulary_list(key = 'x', 
                                                 vocabulary_list = ['a','b','c']))]
model = tf.estimator.LinearClassifier(feature_columns=indicator_features,
                                                              model_dir = "/tmp/samplemodel")
training_input_fn = tf.estimator.inputs.pandas_input_fn(x = X,
                                                    y=Y,
                                                    batch_size=64,
                                                    shuffle= True,
                                                    num_epochs = None)

model.train(input_fn=training_input_fn,steps=1000)

我已经能够通过在tensorflow中启用渴望执行来打印这些值。 在下面发布我的解决方案。欢迎您提出任何其他想法

import tensorflow as tf
import tensorflow.feature_column as fc 
import pandas as pd

PATH = "/tmp/sample.csv"

tf.enable_eager_execution()

COLUMNS = ['education','label']
train_df = pd.read_csv(PATH, header=None, names = COLUMNS)

#train_df['education'] = train_df['education'].str.split(" ")
def easy_input_function(df, label_key, num_epochs, shuffle, batch_size):
  label = df[label_key]
  ed = tf.string_split(df['education']," ")
  df['education'] = ed
  ds = tf.data.Dataset.from_tensor_slices((dict(df),label))
  if shuffle:
    ds = ds.shuffle(10000)
  ds = ds.batch(batch_size).repeat(num_epochs)
  return ds

ds = easy_input_function(train_df, label_key='label', num_epochs=5, shuffle=False, batch_size=5)


for feature_batch, label_batch in ds.take(1):
  print('Some feature keys:', list(feature_batch.keys())[:5])
  print()
  print('A batch of education  :', feature_batch['education'])
  print()
  print('A batch of Labels:', label_batch )
  print(feature_batch)

education_vocabulary_list = [
    'Bachelors', 'HS-grad', '11th', 'Masters', '9th', 'Some-college',
    'Assoc-acdm', 'Assoc-voc', '7th-8th', 'Doctorate', 'Prof-school',
    '5th-6th', '10th', '1st-4th', 'Preschool', '12th']  
education = tf.feature_column.categorical_column_with_vocabulary_list('education', vocabulary_list=education_vocabulary_list)

fc.input_layer(feature_batch, [fc.indicator_column(education)])

我已经能够通过在tensorflow中启用渴望执行来打印这些值。 在下面发布我的解决方案。欢迎您提出任何其他想法

import tensorflow as tf
import tensorflow.feature_column as fc 
import pandas as pd

PATH = "/tmp/sample.csv"

tf.enable_eager_execution()

COLUMNS = ['education','label']
train_df = pd.read_csv(PATH, header=None, names = COLUMNS)

#train_df['education'] = train_df['education'].str.split(" ")
def easy_input_function(df, label_key, num_epochs, shuffle, batch_size):
  label = df[label_key]
  ed = tf.string_split(df['education']," ")
  df['education'] = ed
  ds = tf.data.Dataset.from_tensor_slices((dict(df),label))
  if shuffle:
    ds = ds.shuffle(10000)
  ds = ds.batch(batch_size).repeat(num_epochs)
  return ds

ds = easy_input_function(train_df, label_key='label', num_epochs=5, shuffle=False, batch_size=5)


for feature_batch, label_batch in ds.take(1):
  print('Some feature keys:', list(feature_batch.keys())[:5])
  print()
  print('A batch of education  :', feature_batch['education'])
  print()
  print('A batch of Labels:', label_batch )
  print(feature_batch)

education_vocabulary_list = [
    'Bachelors', 'HS-grad', '11th', 'Masters', '9th', 'Some-college',
    'Assoc-acdm', 'Assoc-voc', '7th-8th', 'Doctorate', 'Prof-school',
    '5th-6th', '10th', '1st-4th', 'Preschool', '12th']  
education = tf.feature_column.categorical_column_with_vocabulary_list('education', vocabulary_list=education_vocabulary_list)

fc.input_layer(feature_batch, [fc.indicator_column(education)])

你能在sklearn中发布你是怎么做的,这样我就可以尝试复制它了吗?你试过tf.Print()吗?@Sharky。。。我不明白这里如何使用tf.Print()。。有什么想法吗?@gorjan。。我可以简单地做一些像这个例子@KundanKumar,你可以传递任何张量给它。我不确定它是否能与tf.estimator.inputs.pandas_input_fn一起使用,但你可以尝试像tf.Print(x,[x])一样将你的“x”传递给它。你能在sklearn中发布你将如何执行它,以便我可以尝试复制它吗?你尝试过tf.Print()?@Sharky。。。我不明白这里如何使用tf.Print()。。有什么想法吗?@gorjan。。我可以简单地做一些像这个例子@KundanKumar,你可以传递任何张量给它。我不确定它是否能与tf.estimator.inputs.pandas\u input\u fn一起使用,但您可以尝试像tf.Print(x,[x])一样将您的“x”传递给它