我收到此WARNING
警告:tensorflow:不推荐使用create_partitioned_variables。将tf.get_variable与分区器集或tf.get_partitioned_variable_list一起使用。
我没有使用函数create_partitioned_variables
我正在使用tensorflow.__version__ 0.9.0
解决此问题的最合适方法是什么WARNING
如果你和pandas / numpy一起安装了tensorflow,你应该可以复制并粘贴这段代码。
import pandas as pd
import numpy as np
import tensorflow as tf
import tempfile
np.random.seed([3,1415])
df = pd.DataFrame(dict(cat1=np.random.choice(('Yes', 'No'), (100,),),
val1=np.random.rand(100),
val2=np.random.rand(100),
val3=np.random.rand(100),
label=np.random.choice((0, 1), (100,))))
LABEL_COLUMN = "label"
trainBegin, trainEnd = 0, 80
testBegin, testEnd = 80, 100
df_train = df.iloc[trainBegin:trainEnd, :]
df_test = df.iloc[testBegin:testEnd, :]
CONTINUOUS_COLUMNS = ['val1', 'val2', 'val3']
CATEGORICAL_COLUMNS = ['cat1']
def input_fn(df):
# Creates a dictionary mapping from each continuous feature column name (k) to
# the values of that column stored in a constant Tensor.
continuous_cols = {k: tf.constant(df[k].values)
for k in CONTINUOUS_COLUMNS}
# Creates a dictionary mapping from each categorical feature column name (k)
# to the values of that column stored in a tf.SparseTensor.
categorical_cols = {k: tf.SparseTensor(
indices=[[i, 0] for i in range(df[k].size)],
values=df[k].values,
shape=[df[k].size, 1])
for k in CATEGORICAL_COLUMNS}
# Merges the two dictionaries into one.
feature_cols = dict(continuous_cols.items() + categorical_cols.items())
# Converts the label column into a constant Tensor.
label = tf.constant(df[LABEL_COLUMN].values)
# Returns the feature columns and the label.
return feature_cols, label
def train_input_fn():
return input_fn(df_train)
def eval_input_fn():
return input_fn(df_test)
val1 = tf.contrib.layers.real_valued_column("val1")
val2 = tf.contrib.layers.real_valued_column("val2")
val3 = tf.contrib.layers.real_valued_column("val3")
cat1 = tf.contrib.layers.sparse_column_with_keys(column_name="cat1", keys=["Yes", "No"])
wide_columns = [val1, val2, val3, cat1]
当我跑步时, WARNINGS
来了:
model_dir = tempfile.mkdtemp()
m = tf.contrib.learn.LinearClassifier(feature_columns=wide_columns, model_dir=model_dir)
m.fit(input_fn=train_input_fn, steps=200)
results = m.evaluate(input_fn=eval_input_fn, steps=1)
for key in sorted(results):
print("%s: %s" % (key, results[key]))
如果我改为运行:
model_dir = tempfile.mkdtemp()
m = tf.contrib.learn.LinearClassifier(feature_columns=wide_columns[:-1], model_dir=model_dir)
# removing last feature columns which is categorical using [:-1] ^^^^^
m.fit(input_fn=train_input_fn, steps=200)
results = m.evaluate(input_fn=eval_input_fn, steps=1)
for key in sorted(results):
print("%s: %s" % (key, results[key]))
我没有错误。
accuracy: 0.45
eval_auc: 0.459596
loss: 0.771354
我将其缩小为使用.sparse_column_with_keys
创建的分类功能,但我应该怎么做呢?
答案 0 :(得分:2)
基本上sparse_column_with_keys
正在使用create_partitioned_variables()
进行初始化,但是,在create_partitioned_variables()
类中,它们会发出警告,基本上说create_partitioned_variables()
不是可接受的方式再创建分区变量相反,您可以使用get_variable()
功能。
如果您转到tensorflow/contrib/layers/python/layers
并打开_feature_column.py_,您会发现sparse_column_with_keys
的嵌入使用create_partitioned_variables()
。要解决此问题,请导入get_variable function
(from tensorflow.python.ops.variable_scope import get_variable
),然后使用embeddings
而不是{定义变量create_partitioned_variables()
(最初应使用get_variable()
) {1}}。我不认为警告真的很重要。