简体   繁体   English

ValueError: x 和 y 必须具有相同的第一维,但具有形状 (101,) 和 (100,)

[英]ValueError: x and y must have same first dimension, but have shapes (101,) and (100,)

I am trying to run a learning rate, and am getting this error value error: ValueError: x and y must have same first dimension, but have shapes (101,) and (100,).我正在尝试运行学习率,并收到此错误值错误:ValueError:x 和 y 必须具有相同的第一维,但具有形状 (101,) 和 (100,)。 If I make the y 101, it gives a straight line instead如果我使 y 101,它会给出一条直线

def g(x):
    return x**4 - 4*x**2 +5

def dg(x):
    return 4*x**3 - 8*x

def gradient_descent(derivative_func, initial_guess,multiplier=0.01,precision=0.0001, max_iter=500):

    new_x= initial_guess
    precision= 0.0001
    x_list= []
    slope_list= []

    for n in range(max_iter):

        previous_x= new_x
        gradient= derivative_func(previous_x) #The slope of the graph(Error)
        new_x= previous_x - multiplier *gradient #The Learning rate
        step_size= abs(new_x - previous_x)
        x_list.append(new_x)
        slope_list.append(derivative_func(new_x))
       # print (step_size)
        if step_size < precision:
            break
    return new_x, x_list, slope_list

n=100
local_min, list_x, deriv_list= gradient_descent(derivative_func=dg,initial_guess=0.1, multiplier=0.0005,precision=0.0001,max_iter=n)



#Plotting Reduction in cost for each iteration
plt.figure(figsize=[15,5])

plt.xlim(0,n)
plt.ylim(0,50)
plt.xlabel('Nr. of Iterations', fontsize= 16)
plt.ylabel('Cost', fontsize=16)
plt.title('Effect of Learning Rate', fontsize=16, c= 'g')

#Getting X-axis values:

iteration_list1= list(range(0,n+1))
iteration_list= np.array(iteration_list1)

#Getting y-axis values

low_values= np.array(list_x) 

plt.plot(iteration_list, g(low_values),linewidth=6,c='green')
plt.show()

First, you have to have the same array dimensions, so change this line :首先,您必须具有相同的数组维度,因此更改此行:

iteration_list1= list(range(0,n+1))

to

iteration_list1= list(range(0,n))

Then, I think you should delete the line然后,我认为您应该删除该行

plt.ylim(0,50)

to let the library set the best y scale.让图书馆设置最佳 y 比例。 You see a "straight line" because the variations are very small.您会看到一条“直线”,因为变化非常小。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:x 和 y 必须具有相同的第一维,但具有形状 (1, 2) 和 (2,) - ValueError: x and y must have same first dimension, but have shapes (1, 2) and (2,) ValueError:x 和 y 必须具有相同的第一维,但具有形状 - ValueError: x and y must have same first dimension, but have shapes ValueError:x 和 y 必须具有相同的第一维,但具有形状 (6,) 和 (8,) - ValueError: x and y must have same first dimension, but have shapes (6,) and (8,) 线性回归模型形状 - ValueError:x 和 y 必须具有相同的第一维,但具有形状 (5,) 和 (1, 5) - Linear regression model shapes - ValueError: x and y must have same first dimension, but have shapes (5,) and (1, 5) 为什么我不断得到“x 和 y 必须具有相同的第一维,但具有形状 (100,) 和 (1, 100)”? - Why do I keep on getting “x and y must have same first dimension, but have shapes (100,) and (1, 100)”? ValueError:x和y必须具有相同的第一尺寸,但形状为(4200,)和(16800,1) - ValueError: x and y must have same first dimension, but have shapes (4200,) and (16800, 1) ValueError: x 和 y 必须具有相同的第一维,但具有形状 (41,) 和 (1, 41) - ValueError: x and y must have same first dimension, but have shapes (41,) and (1, 41) ValueError: x 和 y 必须具有相同的第一维,但具有形状 (10, 1) 和 (90,) - ValueError: x and y must have same first dimension, but have shapes (10, 1) and (90,) 线性回归:ValueError:x 和 y 必须具有相同的第一维,但具有形状 (10, 1) 和 (1, 1) - Linear Regression : ValueError: x and y must have same first dimension, but have shapes (10, 1) and (1, 1) ValueError:x 和 y 必须具有相同的第一维,但具有形状 (165,) 和 (166,) - ValueError: x and y must have same first dimension, but have shapes (165,) and (166,)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM