我想训练一个具有张量流的模型,该模型具有如下布尔特征:
data = np.array([[0,0,0],[0,0,1],[0,1,0],[1,0,0],[1,0,1],[1,1,1]], dtype=bool)
target = np.array([0,1,2,3,4,5], dtype=np.int )
对我来说看起来很简单,但事实证明,对我来说这不是一件容易的事。我不知道如何做到这一点,在网络上找不到类似的东西(除了this),我无法根据我的需要调整其中一个张量流示例。
O.K。这是代码......
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
import numpy as np
import collections
# Data sets
dataComplete = np.array([[0,0,0],[0,0,1],[0,1,0],[0,1,1],[1,0,0],[1,0,1],[1,1,0],[1,1,1]], dtype=bool)
targetComplete = np.array([0, 1, 2, 3, 4, 5, 6, 7 ], dtype=np.int )
Dataset = collections.namedtuple('Dataset', ['data', 'target'])
# for trainig data, remove some data from the complete set
data = np.delete(dataComplete, [2,4,6], 0)
target = np.delete(targetComplete, [2,4,6])
training_set = Dataset(data=data, target=target)
# for test set pick some of the complete set.
data = np.array([[0,1,0], [1,0,0]], dtype=bool)
target = np.array([2,4], dtype=np.int )
test_set = Dataset(data=data, target=target)
# Specify that all features have real-value data
# <-- This is seems to be wrong. I do not have a real valued featured, but boolean features. However
# I could not something like tf.contrib.layers.boolean_valued_column
feature_columns = [tf.contrib.layers.real_valued_column("", dimension=3)]
# Build 3 layer DNN with 10, 20, 10 units respectively.
#classifier = tf.contrib.learn.DNNClassifier(feature_columns=feature_columns,
# hidden_units=[10, 20, 10],
# n_classes=8,
# model_dir="/tmp/chesspositions_model")
classifier = tf.contrib.learn.LinearClassifier(feature_columns)
# Fit model.
classifier.fit(x=training_set.data,
y=training_set.target,
steps=100)
# Evaluate accuracy.
accuracy_score = classifier.evaluate(x=test_set.data,
y=test_set.target)["accuracy"]
print('Accuracy: {0:f}'.format(accuracy_score))
data = np.array([[0,0,1], [0,1,1]], dtype=bool); target = np.array([1,3], dtype=np.int)
),我得到的准确度为:0.500000 我不明白这些结果:-o 我原本预计第一步的准确度会有一些,每增加一步,精度达到1.0。而且我希望这种方法对于析取训练和测试集来说要慢一些。
steps=1
中的参数classifier.fit
似乎没有任何效果。