neural network - tensorflow tf getting error cross entropy in built function -


i have 2 functions below, come andrew ng deep learning course on coursera. first function runs, second doesn't. logits , labels variables have same shape per document requirements changed cost [0.0,0.0,1.0,1.0], didn't :(

in case of first function directly passing variables function call function

1)

def one_hot_matrix(labels, c):     """     creates matrix i-th row corresponds ith class number , jth column                      corresponds jth training example. if example j had label i. entry (i,j)                       1.       arguments:     labels -- vector containing labels      c -- number of classes, depth of 1 hot dimension      returns:      one_hot -- 1 hot matrix     """      ### start code here ###      # create tf.constant equal c (depth), name 'c'. (approx. 1 line)     #c = tf.constant(c, name = 'c')     #c = tf.placeholder(tf.int32, name = 'c')     #labels = tf.placeholder(tf.int32, name = 'labels')      # use tf.one_hot, careful axis (approx. 1 line)     one_hot_matrix = tf.one_hot(labels, c, axis=0)      # create session (approx. 1 line)     sess = tf.session()      # run session (approx. 1 line)     #one_hot = sess.run(one_hot_matrix)     one_hot = sess.run(one_hot_matrix)      # close session (approx. 1 line). see method 1 above.     sess.close()      ### end code here ###      return one_hot  labels = np.array([1,2,3,0,2,1]) one_hot = one_hot_matrix(labels, c = 4) print ("one_hot = " + str(one_hot)) 

2)

    def cost(logits, labels):     """     computes cost using sigmoid cross entropy          arguments:     logits -- vector containing z, output of last linear unit (before final sigmoid activation)     labels -- vector of labels y (1 or 0)       note: we've been calling "z" , "y" in class respectively called "logits" , "labels"      in tensorflow documentation. logits feed z, , labels y.           returns:     cost -- runs session of cost (formula (2))     """      ### start code here ###       # create placeholders "logits" (z) , "labels" (y) (approx. 2 lines)     z = tf.placeholder(tf.float32, name = 'z')     y = tf.placeholder(tf.float32, name = 'y')        # use loss function (approx. 1 line)     #cost = tf.nn.sigmoid_cross_entropy_with_logits(logits = z,  labels = y)     cost = tf.nn.sigmoid_cross_entropy_with_logits(logits=logits,  labels=labels)      # create session (approx. 1 line). see method 1 above.     sess = tf.session()      # run session (approx. 1 line).     #cost = sess.run(cost, feed_dict = {z: logits, y:labels})     cost = sess.run(cost)      # close session (approx. 1 line). see method 1 above.     sess.close()      ### end code here ###      return cost  logits = sigmoid(np.array([0.2,0.4,0.7,0.9])) cost = cost(logits, np.array([0,0,1,1])) print ("cost = " + str(cost)) 

the error

 --------------------------------------------------------------------------- valueerror                                traceback (most recent call last) <ipython-input-61-51f13e22d2ec> in <module>()       1 logits = sigmoid(np.array([0.2,0.4,0.7,0.9])) ----> 2 cost = cost(logits, np.array([0,0,1,1]))       3 print ("cost = " + str(cost))  <ipython-input-60-3febf014323d> in cost(logits, labels)      26     # use loss function (approx. 1 line)      27     #cost = tf.nn.sigmoid_cross_entropy_with_logits(logits = z,  labels = y) ---> 28     cost = tf.nn.sigmoid_cross_entropy_with_logits(logits=logits,  labels=labels)      29       30     # create session (approx. 1 line). see method 1 above.  /opt/conda/lib/python3.6/site-packages/tensorflow/python/ops/nn_impl.py in sigmoid_cross_entropy_with_logits(_sentinel, labels, logits, name)     169     relu_logits = array_ops.where(cond, logits, zeros)     170     neg_abs_logits = array_ops.where(cond, -logits, logits) --> 171     return math_ops.add(relu_logits - logits * labels,     172                         math_ops.log1p(math_ops.exp(neg_abs_logits)),     173                         name=name)  /opt/conda/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py in binary_op_wrapper(x, y)     827       if not isinstance(y, sparse_tensor.sparsetensor):     828         try: --> 829           y = ops.convert_to_tensor(y, dtype=x.dtype.base_dtype, name="y")     830         except typeerror:     831           # if rhs not tensor, might tensor aware object  /opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in convert_to_tensor(value, dtype, name, preferred_dtype)     674       name=name,     675       preferred_dtype=preferred_dtype, --> 676       as_ref=false)     677      678   /opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype)     739      740         if ret none: --> 741           ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)     742      743         if ret notimplemented:  /opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in _tensortensorconversionfunction(t, dtype, name, as_ref)     612     raise valueerror(     613         "tensor conversion requested dtype %s tensor dtype %s: %r" --> 614         % (dtype.name, t.dtype.name, str(t)))     615   return t     616   valueerror: tensor conversion requested dtype float32 tensor dtype int64: 'tensor("logistic_loss_4/labels:0", shape=(4,), dtype=int64)' 

is not problem you've commented lines create c tensorflow constant? try uncomment line again , add c value. should this:

c = tf.constant(c,tf.int32, name = "c") 

so assign value parameter tensorflow constant c.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -