ValueError: The truth value of an array with more than one element is ambivalent.Use a.any() or a.all() error appears.

Asked 2 years ago, Updated 2 years ago, 36 views

This is my first time asking a question.I'm a beginner, but I appreciate your cooperation
If you run the program below, you will see the results below, and you will not see any improvements.
I'm asking for some advice. Thank you very much.
The input data file is

 12.1
42.2
32.2
15.2

The data is like
I would like to output the same output.

#-*-coding:utf-8-*-

import tensorflow as tf# Import tensorflow which is library for NN

import numpy as np# Import numpy which is library for calculation

'' ------------------------------------------------------------------------------------------
 # Number of units in the input layer
num_units1 = 10 # Number of units in intermediate layer 1

x = tf.placeholder(tf.float32,[1720])#Input Layer

w_enc = tf.Variable(tf.random_normal([720, num_units1], stddev = 0.01))#Input side weight
b_enc=tf.Variable(tf.zeros([num_units1])))#Input side bias

w_dec=tf.Variable(tf.random_normal([num_units1720], stddev=0.01))# Output Weight
b_dec=tf.Variable(tf.zeros([720])))# Output Bias

encoded = tf.nn.relu(tf.matmul(x,w_enc)+b_enc)#Input side activation function (ReLU)

# drop = tf.nn.dropout(encoded, 0.5) # dropout

decoded = tf.matmul(encoded, w_dec)+b_dec# Output side activation function (equal mapping)

lambda2 = 0.1 # regularization factor
l2_norm=tf.nn.l2_loss(w_enc)+tf.nn.l2_loss(w_dec)#L2 regularization
loss1 = tf.reduce_sum(tf.square(x-decoded)) + lambda2*l2_norm # loss function (square error function)

loss2 = tf.abs(tf.reduce_sum(decoded-x)/tf.reduce_sum(x)) * 100 # percentage error

train_step=tf.train.AdamOptimizer(learning_rate=0.0001).minimize(loss1)# Gradient Descent Method(Adam)

US>' ------------------------------- Initialize variables and load VLF data -----------------'

sess=tf.InteractiveSession()#Define session start as variable
sess.run(tf.global_variables_initializer())#variable initialization

a=np.loadtxt("train_datasin125.txt")# Load training data

b=np.loadtxt("sintest_data11.txt")# Load test data 1

c=np.loadtxt("sintast_data2.txt")# Load test data 2

US>' --------------------------------------------------------------------------------------

for step in range (1000000)—# Number of times learned
    r=a[np.random.randint(0,2000, [1]]]# Randomly select one day's worth of data from training data

    sess.run(train_step, feed_dict={x:r})#Learning session started
    if step %49 == 0: # Every time you learn 50 times
        loss_val=sess.run(loss2, feed_dict={x:r})# Calculate the error expressed as a percentage
        print('Step:%d, Loss:%f'%(step,loss_val))# Show current number of studies and errors

    if step %499 == 0: # Every 500 times you learn

        US>' ------------------------------------------------------------------------------------------

        if step == 0:
            out_g=step+1
        else:
            out_g = np.column_stack([out_g, step+1])

        US>' --------------------------------------------------------------------------------
        max = 0
        for i in range (2000):
            r=a[i,:]
            r=r.reshape(1720)
            loss_val = sess.run (loss2, feed_dict = {x:r})
            if max<loss_val:
                max = loss_val
            if i == 1999:
                if step == 0:
                    out1 = max
                else:
                    out1 = np.column_stack ([out1,max])

        US>' --------------------------------------------------------------------------------

        max = 0
        for q in range (30):
            r=c[q,:]
            r=r.reshape(1720)
            loss_val = sess.run (loss2, feed_dict = {x:r})
            if max<loss_val:
                max = loss_val
            if q == 29:
                if step == 0:
                    out3 = max
                else:
                    out3 = np.column_stack ([out3,max])

        US>' -----------------------------------------------------------------------------------

        max = out3
        count = 0
        for pin range (48):
            r=b[p,:]
            r=r.reshape(1720)
            loss_val = sess.run (loss2, feed_dict = {x:r})
            if max<loss_val:
                count = count +1
            ifp == 47:
                if step == 0:
                    out2 = max
                    out4 = count
                else:
                    out2 = np.column_stack ([out2,max])
                    out4 = np.column_stack ([out4, count])

US>' ----------------------------------------------------------------------------

out = np.column_stack ([out_g.T, out1.T, out2.T, out3.T])
np.savetxt("sinoutput110.txt", out)

out = np.column_stack ([out4.T])
np.savetxt("sinoutput010.txt", out)

Run Results

/Users/ishidatakuma/PycharmProjects/sotsuken/venv/bin/python/Users/ishidatakuma/PycharmProjects/sotsuken/jikkenn2.py
Step: 0, Loss: 147.256897
Step:49, Loss:2897.626221
Step: 98, Loss: 18581.408203
Step: 147, Loss: 33576.679688
Step: 196, Loss: 36586.777344
Step: 245, Loss: 20837.001953
Step: 294, Loss: 101844.562500
Step: 343, Loss: 16776.875000
Step: 392, Loss: 5594.307129
Step:441, Loss:2011.017578
Step:490, Loss: 5526.880859
Traceback (most recent call last):
  File"/Users/ishidatakuma/PycharmProjects/sotsuken/jikkenn2.py", line 107, in<module>
    if yosino<loss_val:
ValueError: The true value of an array with more than one element is ambivalent.Use a.any() or a.all()

python python3

2022-09-30 10:37

1 Answers

Perhaps yosinoloss_val or both are lists or arrays?
I think it's because you're trying to compare it with a simple expression even though it contains more than one value.
Wouldn't it be better to use or create a function or subroutine that can be strictly compared or judged?

The explanation in the article around here seems to be true.
I am a beginner at Python. Define a function with conditional branches...

value error and I don't know what to do.

About errors when comparing multi-dimensional arrays of numpy

Use a.any() or a.all()


2022-09-30 10:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.