<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Ask Ghassem - Recent questions tagged dnnclassifier</title>
<link>https://ask.ghassem.com/tag/dnnclassifier</link>
<description>Powered by Question2Answer</description>
<item>
<title>Using Tensorflow.DNNClassifier, getting Error: assertion failed: [Labels must &gt;= 0]</title>
<link>https://ask.ghassem.com/440/tensorflow-dnnclassifier-getting-assertion-failed-labels</link>
<description>&lt;p&gt;Hi All,&lt;/p&gt;

&lt;p&gt;I am writing a simple program using Tensorflow and DNNClassifier. Training Data is 9 pixel with four spectral bands, i.e. 4*9=36 featurs. And each data-point will be mapped to a class (from 1 to 7).&amp;nbsp;&lt;/p&gt;

&lt;p&gt;Last parameter, is the class label.&lt;/p&gt;

&lt;p&gt;A line of data-point is like this:&lt;/p&gt;

&lt;pre&gt;
67,75,77,62,67,79,81,62,75,87,89,71,66,79,88,63,66,79,84,63,66,79,80,59,67,84,86,68,71,84,86,64,67,81,82,64,7&lt;/pre&gt;

&lt;p&gt;But I got below Error:&lt;/p&gt;

&lt;pre class=&quot;prettyprint lang-python&quot; data-pbcklang=&quot;python&quot; data-pbcktabsize=&quot;4&quot;&gt;
InvalidArgumentError (see above for traceback): assertion failed: [Labels must &amp;gt;= 0] [Condition x &amp;gt;= 0 did not hold element-wise:] [x (dnn/head/labels:0) = ] [[3][3][3]...]&lt;/pre&gt;

&lt;p&gt;I am sure there is no datapoint&amp;nbsp;which has a label&amp;nbsp;less than 0. Would you please advise?&lt;/p&gt;

&lt;pre class=&quot;prettyprint lang-python&quot; data-pbcklang=&quot;python&quot; data-pbcktabsize=&quot;4&quot;&gt;
import numpy as np

import pandas as pd

import tensorflow as tf

from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import GridSearchCV, KFold
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn.model_selection import StratifiedShuffleSplit

print(&#039;** DNN Classification *******************************************************&#039;)

landsatData = pd.read_csv(&quot;./resources/landsat/lantsat.1.csv&quot;)

landsatData.describe()

X_landSatAllFeatures = landsatData.iloc[:, np.arange(36)].copy()

y_midPixelAsTarget = landsatData.iloc[:, 36].copy()

# Testing and training sentences splitting (stratified + shuffled) based on the index (sentence ID)
allFeaturesIndexes = X_landSatAllFeatures.index
targetData = y_midPixelAsTarget
sss = StratifiedShuffleSplit(n_splits=1, test_size=0.3, random_state=42)

for train_index, test_index in sss.split(allFeaturesIndexes, targetData):
    train_ind, test_ind = allFeaturesIndexes[train_index], allFeaturesIndexes[test_index]

Test_Matrix = X_landSatAllFeatures.loc[test_ind]
Test_Target_Matrix = y_midPixelAsTarget.loc[test_ind]
Train_Matrix = X_landSatAllFeatures.loc[train_ind]
Train_Target_Matrix = y_midPixelAsTarget.loc[train_ind]

scaler = StandardScaler().fit(Train_Matrix)
Train_Matrix, Test_Matrix = scaler.transform(Train_Matrix), scaler.transform(Test_Matrix)

def reset_graph(seed=42):
    tf.reset_default_graph()
    tf.set_random_seed(seed)
    np.random.seed(seed)

X_train = Train_Matrix
y_train = Train_Target_Matrix
X_test = Test_Matrix
y_test = Test_Target_Matrix

xx, yy = Train_Matrix.shape
#training phase
feature_cols = [tf.feature_column.numeric_column(&quot;X&quot;, shape=[36])]
dnn_clf = tf.estimator.DNNClassifier(hidden_units=[300,100], n_classes=8, feature_columns=feature_cols)
# dnn_clf = tf.estimator.DNNClassifier(hidden_units=[300,100], n_classes=10)


input_fn = tf.estimator.inputs.numpy_input_fn(
    x={&quot;X&quot;: X_train}, y=y_train, num_epochs=40, batch_size=64, shuffle=True)
dnn_clf.train(input_fn=input_fn)

#testing phase
test_input_fn = tf.estimator.inputs.numpy_input_fn(
    x={&quot;X&quot;: X_test}, y=y_test, shuffle=False)
eval_results = dnn_clf.evaluate(input_fn=test_input_fn)
print(&quot;The prediction result is : {0:.2f}%&quot;.format(100*eval_results[&#039;accuracy&#039;]))
y_pred_iter = dnn_clf.predict(input_fn=test_input_fn)
y_pred = list(y_pred_iter)
y_pred[0]


print(&#039;**********************************************************************************&#039;)&lt;/pre&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description>
<category>Deep Learning</category>
<guid isPermaLink="true">https://ask.ghassem.com/440/tensorflow-dnnclassifier-getting-assertion-failed-labels</guid>
<pubDate>Wed, 24 Oct 2018 03:12:33 +0000</pubDate>
</item>
</channel>
</rss>