我收到了这个警告
WARNING:tensorflow:create_partitioned_variables is deprecated. Use tf.get_variable with a partitioner set, or tf.get_partitioned_variable_list, instead.
我没有使用create_partitioned_variables函数
我正在使用tensorflow .__ version__ 0.9.0
如果你安装了tensorflow和pandas / numpy,你应该能够复制并粘贴这段代码.
建立
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]
我跑的时候会发出警告:
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创建的分类功能,但我应该怎么做呢?
解决方法:
基本上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函数(来自tensorflow.python.ops.variable_scope import get_variable),然后使用get_variable()而不是create_partitioned_variables()定义变量嵌入(最初应使用create_partitioned_variables()).我不认为警告真的很重要.