A variable like user_num can store a value like an integer. Extend the given program as indicated.
Output the user's input. (2 pts)
Output the input squared and cubed. Hint: Compute squared as user_num * user_num. (2 pts)
Get a second user input into user_num2, and output the sum and product. (1 pt)
Given:
user_num = int(input('Enter integer:\n'))
Type your code here
I have no idea where to start. I have looked through my resources and I dont know which coding formula to follow.
user_num = int(input('Enter integer:\n'))
print(user_num) # Output the user's input
print(user_num * user_num) # Output the input squared
print(user_num * user_num * user_num) # Output the input cubed
user_num2 = int(input('Enter another integer:\n')) # Get a second user input into user_num2
print(user_num + user_num2) # output the sum
print(user_num * user_num2) # output the product
Related
I'm plotting a facebook-prophet forecast and I only want to plot the last month data. This 'last month' changes from month to month so I can't use plt.xlim() to limit it by a given range.
Is there any way to limit the x-axis by a given number of elements, like plot last 1000 x-axis values no matter what those values are?
I'd say write a function that represents your understanding of the appropriate x limits. For example:
def get_x_limits(data):
lower_x = min(data)
# or
lower_x = min(data)*0.9 # 10% below lowest value (if values are positive)
# or
lower_x = min(data) - 1 # a little below lowest value
# similarly
upper_x = max(data)
# or
upper_x = max(data)*1.1 # 10% above highest value (if values are positive)
# or
upper_x = max(data) + 1 # a little above highest value
# maybe smth like this
if upper_x - lower_x < 1000:
upper_x = lower_x + 1000
# and finally
return lower_x, upper_x
You can then use these values to set your limits:
lower_x, upper_x = get_x_limits(data)
plt.xlim(lower_x,upper_x)
I have to admit that I ignored the question for the number of elements since I thought its mostly relevant what's in your data, not how much data you have. However, you can still work len(data) into the get_x_limits function the way it fits your need.
In this transformer tutorial:
https://www.tensorflow.org/text/tutorials/transformer
def get_angles(pos, i, d_model):
angle_rates = 1 / np.power(10000, (2 * (i//2)) / np.float32(d_model))
return pos * angle_rates
I don't understand why 'i//2' is used, since in the original formula there is no specification of the integer division.
So what's the purpose of i//2?
according to the formula
PE(pos, 2i) = sin(1/100000^(2i /D) , PE(pos, 2i+1) = cos(1/100000^(2i
/D)
As you can see, for odd row, 2i -> 2i, for even row 2i+1 -> 2i, for example, a word embedding with [0,1,2,3,4,5,6,7] should transfer to [0,0,1,1,2,2,3,3], there is where i//2 comes from.
I presume it is some kind of moving average, but the valid range is between 0 and 1.
It is called exponential moving average, below is a code explanation how it is created.
Assuming all the real scalar values are in a list called scalars the smoothing is applied as follows:
def smooth(scalars: List[float], weight: float) -> List[float]: # Weight between 0 and 1
last = scalars[0] # First value in the plot (first timestep)
smoothed = list()
for point in scalars:
smoothed_val = last * weight + (1 - weight) * point # Calculate smoothed value
smoothed.append(smoothed_val) # Save it
last = smoothed_val # Anchor the last smoothed value
return smoothed
Here is the actual piece of source code that performs that exponential smoothing the with some additional de-biasing explained in the comments to compensate for the choice of the zero initial value:
last = last * smoothingWeight + (1 - smoothingWeight) * nextVal
Source: https://github.com/tensorflow/tensorboard/blob/34877f15153e1a2087316b9952c931807a122aa7/tensorboard/components/vz_line_chart2/line-chart.ts#L714
The implementation of EMA smoothing used for TensorBoard can be found here.
The equivalent in Python is actually:
def smooth(scalars: list[float], weight: float) -> list[float]:
"""
EMA implementation according to
https://github.com/tensorflow/tensorboard/blob/34877f15153e1a2087316b9952c931807a122aa7/tensorboard/components/vz_line_chart2/line-chart.ts#L699
"""
last = 0
smoothed = []
num_acc = 0
for next_val in scalars:
last = last * weight + (1 - weight) * next_val
num_acc += 1
# de-bias
debias_weight = 1
if weight != 1:
debias_weight = 1 - math.pow(weight, num_acc)
smoothed_val = last / debias_weight
smoothed.append(smoothed_val)
return smoothed
How can I force certain dimensionality of the output of the conv2d_transpose layer ? My problem is that I use it for upsampling and I want to match the dimensionality of my labels and the output of the NN, for example if I have a feature map as Bx25x40xC how can I make it Bx100x160xC (i.e. upsample exactly 4x times)?
It seems like dimensions of the output can be calculated using
h = ((h_in - 1) * stride_h) + kernel_h - 2 * pad_h
w = ((w_in - 1) * stride_w) + kernel_w - 2 * pad_w
one can manipulate strides and kernels, but padding is controlled by 'same'/'valid' algorithms which, to my understanding, means they are pretty much uncontrollable, so is the resulting output size. For comparison, in caffe, one can at least force the padding in attempt to match the desired output explicitly.
'm new to mathematica. i have a small project: get an equation , a number n, a number x and a number y and a number h, then using euler formula calculate nTh iteration ... My code is :
f[x_,y_]=input["Please input f(x,y):"]
n=input["Please input number of iterations:"]
x0=input["Please input initial value x0:"]
y0=input["Please input initial value y0:"]
h=input["please input h:"]
For[i=0,i<n,i++,y0=y0+f[x0,y0]*h;x0=x0+h]
but when i copy this code in mathematica 9; it just print some texts and end . it did not get any input from user.just print this:
input["Please input f(x,y):"]
input["Please input number of iterations:"]
input["Please input initial value x0:"]
input["Please input initial value y0:"]
input["please input h:"]
and then end!
whould you help me ?
You should write it in two separate parts.
I would write an initialize variables part then the for loop.
Functions in Mathematica need a delayed equals := and to you recieved the outputs you did because Mathematica doesn't allow you to input while the code is running. you also should suppress the output with a ;
f[x_,y_]:= ; %%write in f(x,y)
n= ; %%input number of iterations
x0= ; %%input initial value x0
y0= ; %%input initial value y0
h= ; %%input h
An euler form of the solution is
euler:= Module[{ans, i, x, y, nsteps},
ans = {{x0,y0}};x=x0;y=y0;nsteps=n;
Do[(y=y+h*f[x,y];
x=x+h; ans=Append[and,{x,y}]),{i,1,nsteps}];ans]
To view the euler forumal result do...
eulerans1 = euler