how to approach leaf variables to freeze weight parameters - pytorch

i want to fix weights which have a value of 'zero' in model.parameters() (model is a neural network)
it means i have to freeze it in element-wise so i made a code below.
in the code, model.parameters() will be input of 'Lparam'
and when i ran the code, i got an error like 'nonetype has no attribute 'zero_()'
and when i try another trick like requires_grad=False, error says like,
"only leaf variables can use requires_grad"
then how can i approach leaf variables to fix the zero weights in the computation graph?
here is my code.
def fix_tensor_data(Lparam):
if isinstance(Lparam,type(None)):
return
elif torch.is_tensor(Lparam):
if Lparam.dim() == 0 and Lparam == 0:
Lparam.grad.zero_()
elif Lparam.dim() == 0 and Lparam != 0:
return
else:
for tp1 in Lparam:
fix_tensor_data(tp1)
else: #enter this section only once when Lparam is a generator
for tp2 in Lparam:
fix_tensor_data(tp2)

Related

Cant output an if statement when using a def function whose values are user inputted

def sum(x, y):
return x+y
x = input()
y = input()
sum(x, y)
if sum == 15:
print("It is equal to 15")
else:
print("It is not equal to 15")
So this code here won't output "it isn't equal to 15" even when I put 15 and 0 for the inputs.
I've tried nesting the if statement under the def code block but that didn't work, said it wasn't reachable.
Im new to the return function btw, so id also appreciate it if you could tell me how to better use it in this code. Thank you.
You are not storing or referencing the result of the sum function. Use one of the two excerpts below to see the result you are looking for.
Call function directly into if statement
if sum(x, y) == 15:
print("It is equal to 15")
else:
print("It is not equal to 15")
Store function result as variable
sumResult = sum(x, y)
if sumResult == 15:
print("It is equal to 15")
else:
print("It is not equal to 15")

fibbonnacci. sequence UnboundLocalError

I've been working on this Fibbonacci sequence function. I am essentially returning a list of the Fibbonacci sequence by growing out a returned list respective to the number sequence that is generated by passing in a particular number. however I'm encountering an error within my for loop at the if statement within that for loop. it states UnboundLocalError: local variable 'new_list' referenced before assignment. Does anyone have a clue how to remedy this? Any help will be appreciated. thanks
def fib_n(num):
if num < 0:
print("You have entered incorrect output")
elif num == 0:
new_list = []
else:
for index in range(1,num):
if index == 1 or index == 2:
new_list.append(1)
continue
new_list.append(new_list[index - 1] + new_list[index - 2])
return new_list

Trying to tie the selection input so that I can press 1,2 or 3 and get my printed text options

def cSelection():
Selection = input()
return Selection
if Selection == 1 :
print('Oxygen levels are normal')
elif Selection == 2:
print('Fuel levels are at medium capacity')
elif Selection == 3:
print('Food is running low, request for new shipment')
The return statement is not in right place. Your if/elif conditions don't running due to the return returns from the function before them. The return should be after the logic (if/elif).
Furthermore the input() function returns a string type but your if/elif contidions wait an integer. It means your should cast the output of input() to integer with int().
I recommend to define an else branch if the input is not 1-3. Like in my below example.
Correct code:
def cSelection():
Selection = int(input("Write a number (1-3): ")) # Input cast to integer.
if Selection == 1 :
print('Oxygen levels are normal')
elif Selection == 2:
print('Fuel levels are at medium capacity')
elif Selection == 3:
print('Food is running low, request for new shipment')
else:
print("Wrong option")
return Selection
return_value = cSelection()
print("Return value of function: {}".format(return_value))
Output:
>>> python3 test.py
Write a number (1-3): 1
Oxygen levels are normal
Return value of function: 1
>>> python3 test.py
Write a number (1-3): 3
Food is running low, request for new shipment
Return value of function: 3
>>> python3 test.py
Write a number (1-3): 5
Wrong option
Return value of function: 5

Dynamic Programming Primitive calculator code optimization

I am currently doing coursera course on algorithms. I have successfully completed this assignment. All test cases passed. My code looks messy and I want to know if there is any thing availiable in Python which can help run my code faster. Thanks
The problem statement is as follows: You are given a primitive calculator that can perform the following three operations with
the current number 𝑥: multiply 𝑥 by 2, multiply 𝑥 by 3, or add 1 to 𝑥. Your goal is given a
positive integer 𝑛, find the minimum number of operations needed to obtain the number 𝑛
starting from the number 1.
# Uses python3
import sys
def optimal_sequence(m):
a=[0,0]
for i in range(2,m+1):
if i%3==0 and i%2==0:
a.append(min(a[i//2],a[i//3],a[i-1])+1)
elif i%3==0:
a.append(min(a[i//3],a[i-1])+1)
elif i%2==0:
a.append(min(a[i//2],a[i-1])+1)
else:
a.append((a[i-1])+1)
return backtrack(a,m)
def backtrack(a,m):
result=[]
result.append(m)
current = m
for i in range(a[-1],0,-1):
if current%3==0 and a[current//3]==(i-1):
current=current//3
result.append(current)
elif current%2==0 and a[current//2]==(i-1):
current = current//2
result.append(current)
elif a[current-1]==(i-1):
current = current-1
result.append(current)
return result
n = int(input())
if n == 1:
print(0)
print(1)
sys.exit(0)
a= (optimal_sequence(n))
print(len(a)-1)
for x in reversed(a):
print(x,end=" ")
I would use a breadth first search for number 1 starting from number n. Keep track of the numbers that were visited, so that the search backtracks on already visited numbers. For visited numbers remember which is the number you "came from" to reach it, i.e. the next number in the shortest path to n.
In my tests this code runs faster than yours:
from collections import deque
def findOperations(n):
# Perform a BFS
successor = {} # map number to next number in shortest path
queue = deque() # queue with number pairs (curr, next)
queue.append((n,None)) # start at n
while True:
curr, succ = queue.popleft()
if not curr in successor:
successor[curr] = succ
if curr == 1:
break
if curr%3 == 0: queue.append((curr//3, curr))
if curr%2 == 0: queue.append((curr//2, curr))
queue.append((curr-1, curr))
# Create list from successor chain
result = []
i = 1
while i:
result.append(i)
i = successor[i]
return result
Call this function with argument n:
findOperations(n)
It returns a list.

iteration over a sequence with an implicit type in Python 3.6

I am trying to iterate over a sequence of numbers. I have this:
from itertools import islice, count
handle = int(input("Please enter a number:")
handler = str(handle)
parameter = []
for i in handler:
parameter.append(i)
print(parameter) #This was for debugging
revised = parameter(count(1[2])) #I'm not sure I'm using the correct syntax here, the purpose is to make revised == parameter[0] and parameter[2]
Ultimately, what I am trying to achieve is to take a sequence of numbers or two, and compare them. For instance, if i[0] == i[1] + i [2] I want to return True, or for that matter if i[0] == i[1] - i[2]. I want the program to iterate over the entire sequence, checking for these types of associations, for instance, 23156 would == true because 2*3 = 6, 2+3 = 5, 5+1 = 6, 2+3+1=6; etc. It's strictly for my own purposes, just trying to make a toy.
When I utilize
revised = parameter(count(1[2])
I am getting an error that says builtins. TYPEERROR, type int is not subscriptable but I explicitly turned the integer input into a string.
Albeit unclear, what you have attempted to describe is hard to explain. It appears to be akin to a Running Total but with restrictions and of various operations, i.e. addition, subtraction and products.
Restrictions
The first two numbers are seeds
The following numbers must accumulate by some operation
The accumulations must progress contiguously
Code
import operator as op
import itertools as it
def accumulate(vals):
"""Return a set of results from prior, observed operations."""
adds = set(it.accumulate(vals)) # i[0] == i[1] + i[2]
muls = set(it.accumulate(vals, op.mul)) # i[0] == i[1] * i[2]
subs = {-x for x in it.accumulate(vals, func=op.sub)} # i[0] == i[1] - i[2]
#print(adds, muls, subs)
return adds | muls | subs
def rolling_acc(vals):
"""Return accumulations by sweeping all contiguous, windowed values."""
seen = set()
for i, _ in enumerate(vals):
window = vals[i:]
if len(window) >= 3:
seen |= accumulate(window)
return seen
def is_operable(vals):
"""Return `True` if rolling operations on contiguous elements will be seen."""
s = str(vals)
nums = [int(x) for x in s]
ahead = nums[2:]
accums = rolling_acc(nums)
#print(ahead, accums)
return len(set(ahead) & accums) == len(ahead)
Tests
assert is_operable(23156) == True
assert is_operable(21365) == False # {2,3} non-contiguous
assert is_operable(2136) == True
assert is_operable(11125) == True

Resources