Ok so I want to replicate this keep[count] = i Pytorch code in Tensorflow, this is happening within a tf.while_loop both count and i are tensors.
keep = tf.Variable(tf.zeros(tf.size(scores), tf.int64))
count = 0
idx = tf.argsort(scores, axis=0)#scores.sort(0) # sort in ascending order
idx = idx[-top_k:]
...
def loop_body(idx, keep, count, ...)
i = idx[-1] # index of current largest val
keep = tf.scatter_update(keep, count, i)
tf.while_loop(loop_cond, loop_body, loop_vars)
This is what I'm trying to do keep = tf.scatter_update(keep, count, i), this unfortunately raises a AttributeError: 'Tensor' object has no attribute '_lazy_read
Related
I have a list of coordinates [xmin, xmax, ymin, ymax] on a list: [[xmin_a, xmax_a, ymin_a, ymax_a], [xmin_b, xmax_b, ymin_b, ymax_b]] and I want to remove the best result. For example, the [xmin_b, xmax_b, ymin_b, ymax_b]. For this, I tried using the listname.index() function but it returns 0. Through some prints I can tell the element is there, what is wrong with my code?
screenshot of the print evidence
This is the code excert that is troubled:
#Loop for each ground truth object compare with all the detections
for obj in groundtruth:
print('\t',obj)
max_iou = 0
for det in detectedlines:
print('\t>',det)
iou = calc_iou(obj, det, img_width, img_height)
if (iou > max_iou):
max_iou = iou
det_store = det
#removes the pred box with the best IoU from list
index_det = detectedlines.index(det_store)
print(index_det)
print(det_store)
print(detectedlines)
detectedlines.remove(index_det)
print(detectedlines)
You could use the enumerate() function which on each loop of the list will yield a tuple such that the 0th index is the index in the list and the 1st index is the element from the list detectedlines itself. You can store the index in the same way you did for max_iou. When you go to remove the element, you should use the pop method as remove tries to find an equal object in the list to remove.
Sorry if this is not exactly what you are looking for.
#Loop for each ground truth object compare with all the detections
for obj in groundtruth:
print('\t',obj)
max_iou = 0
index_det = None
for elem in enumerate(detectedlines):
det = elem[1]
idx = elem[0]
print('\t>',det)
iou = calc_iou(obj, det, img_width, img_height)
if (iou > max_iou):
max_iou = iou
det_store = det
index_det = idx
#removes the pred box with the best IoU from list
index_det = detectedlines.index(det_store)
print(index_det)
print(det_store)
print(detectedlines)
removed_element = detectedlines.pop(index_det)
print(detectedlines)
Why am I getting this error?
ticker = 'NFLX'
price = get_data(ticker, start_date='2020-01-01', end_date=None, index_as_date=bool, interval ='1d')
price.to_csv(r'D:\Python Stuff\pythonProject\NFLX.csv')
df = pd.read_csv('NFLX.csv')
price_list = df['adjclose']
change = price_list.diff(1)
def RSI():
change.dropna(inplace=True)
positive = change.copy()
negative = change.copy()
positive[positive < 0] = 0
negative[negative > 0] = 0
RSI_days = 5
average_gain = positive.rolling(window=RSI_days).mean()
average_loss = abs(negative.rolling(window=RSI_days).mean())
relative_strength = average_gain/average_loss
rsi = 100.0-(100.0/(1.0+relative_strength))
return rsi
relative_strength_index = RSI()
print(relative_strength_index[-6:])
lowest_rsi = min(relative_strength_index[-6:])
print(lowest_rsi)
index_number = relative_strength_index[-6:].index(lowest_rsi)
print('index of lowest rsi = ' + index_number)
When I run the code, my error is "TypeError: 'Int64Index' object is not callable". How can I improve this code to resolve this error?
Replace the parenthesis in index(lowest_rsi) by brackets
index_number = relative_strength_index[-6:].index[lowest_rsi.astype(int)]
print('index of lowest rsi = ' + index_number)
Edit: Your code should probably look something like this:
relative_strength_index = RSI()
print(relative_strength_index[-6:])
index_number = relative_strength_index[-6:].idxmin()
print('index of lowest rsi =', index_number)
relative_strength_index is not a list but is of type pandas.Series.
Therefore, index is a property and not a method like with list, which is where your original error came from and you cannot call that property.
You should instead cast relative_strength_index to a list and the you can proceed to call the index method as shown below.
Also, you should cast index_number to a string before concatenating it.
index_number = list(relative_strength_index[ -6: ]).index(lowest_rsi)
print('index of lowest rsi = ' + str(index_number))
model = LogisticRegression()
model = model.fit(X, y)
test_data = [1,2,3,4,5,6,7,8,9,10,11,12,13]
test_prediction = model.predict_proba(np.array(test_data))
max = -1.0
res = 0
for i in range(test_prediction):
if test_prediction[i]>max:
max = test_prediction[i]
res = i
if res==0:
print('A')
elif res==1:
print('B')
else:
print('C')
Using the above python code I have to predict the probabilities of the 3 possible results (A, B, C).
The probabilities are saved in test_prediction and it can be printed as:
Output: [[ 0.82882588 0.08641236 0.08476175]]
But the remaining part gives an error:
for i in range(test_prediction):
TypeError: only integer scalar arrays can be converted to a scalar index
I want to find the max probability and then display the event that is likely to occur the most (A/B/C).
How to go about this?
You can also use numpy.argmax which will directly give you the index of the largest value.
import numpy as np
#test_prediction is most probably np array only
pred = np.array(test_prediction)
classes_val = np.argmax(pred, axis=1)
for res in class_val:
if res==0:
print('A')
elif res==1:
print('B')
else:
print('C')
The problem in using array in range
In this case you should use length of array range(len(test_prediction))
Also you may simplify your code:
import operator
#...
enum_predict = enumerate(test_prediction)
res = max(enum_predict, key=operator.itemgetter(1))[0]
enumerate convert array to list of tuples (index, item)
key=operator.itemgetter(1) - max function will compare types by second value
You can do something like this:
predict_prob_df = pd.DataFrame(model.predict_proba(test_data))
max_prob = predict_prob_df.apply(max,axis = 1)
predicted_output = pd.DataFrame(model.predict(test_data))
Then you can concat them:
final_frame = pd.concat([max_prob,predicted_output],axis = 1)
This way you do not need to use the for loop, which was causing the error.
I came up with another solution:
for i in range(3):
if np.take(test_prediction, i) > max:
max = np.take(test_prediction, i)
res = i
if res==0:
.....
This worked by accessing the index in test_prediction using np.take
But the solution specified by #Vivek_Kumar seems more correct and efficient.
I wrote this code to filter values from a Dataset that are <= 6.
import tensorflow as tf
import tensorflow.contrib.data as ds
def make_graph():
inits = []
filter_value = tf.constant([6], dtype=tf.int64)
source = ds.Dataset.range(10)
batched = source.batch(3)
batched_iter = batched.make_initializable_iterator()
batched_next = batched_iter.get_next()
inits.append(batched_iter.initializer)
predicate = tf.less_equal(batched_next, filter_value, name="less_than_filter")
true_coordinates = tf.where(predicate)
reshaped = tf.reshape(true_coordinates, [-1])
# need to turn bools into 1 and 0 elsewhere
found = tf.gather(params=batched_next, indices=reshaped)
return found, inits # prepend final tensor
def run_graph(final_tensor, initializers, rounds):
with tf.Session() as sess:
init_ops = (tf.local_variables_initializer(), tf.global_variables_initializer())
sess.run(init_ops)
summary_writer = tf.summary.FileWriter(graph=sess.graph, logdir=".")
while rounds > 0:
for i in initializers:
sess.run(i)
try:
while True:
final_result = sess.run(final_tensor)
p```pythrint("Got result: {r}".format(r=final_result))
except tf.errors.OutOfRangeError:
print("Got out of range error")
rounds -=1
summary_writer.flush()
def run():
final_tensor, initializers = make_graph()
run_graph(final_tensor=final_tensor,
initializers=initializers,
rounds=1)
if __name__ == "__main__":
run()
However, the results are as follows:
Got result: [0 1 2]
Got result: [3 4 5]
Got result: [6]
Got result: []
Got out of range error
Is there a way to filter this empty Tensor? I tried to brainstorm ways to do this, maybe with a tf.while loop, but I'm not sure whether I'm missing something or such an operation (i.e. an OpKernel "dropping" an input by not producing output based on its value) is not possible in Tensorflow.
Keeping only values <= 6 BEFORE batching:
dataset = ds.Dataset.range(10)
dataset = dataset.filter( lambda v : v <= 6 )
dataset = dataset.batch(3)
batched_iter = dataset.make_initializable_iterator()
This will generate batches containing only the data you want. Note that it's generally better to filter out the unwanted data before building the batches. This way, empty tensors will not be generated by the iterator.
So im a real amateur, trying to implement something you may call a sort of 'simplified' version of the naive bayes algorithm in python, and seem to have a lot of trouble [the reason for which is perhaps the fact that im not too sure i completely understand the way the algorithm works..]. I would appreciate any help/suggestions very much though. This is the code I have:
class GaussianNB(object):
def __init__(self):
'''
Constructor
'''
# This variable will hold the gaussian distribution over your data
# In fact, you need a distribution per class for each feature variable.
# This can be done as a list of lists.
self.classmodels_count = {}
self.classmodels = {}
self.classmodelsMeanAndVariance = {}
self.featureTokenCount= 0;
self.featureTypeCount = 0;
def train(self, trainingdata):
for i in trainingdata:
current_class = i[0]
features = i[1]
if self.classmodels.has_key(current_class):
current_class_model = self.classmodels[current_class]
self.classmodels_count[current_class] = self.classmodels_count[current_class] + 1
else:
current_class_model = {}
self.classmodels_count[current_class] = 1
for f in features:
feature = f[0]
value = f[1]
if current_class_model.has_key(feature):
list_of_values = current_class_model[feature]
list_of_values.append(value)
current_class_model[feature] = list_of_values
else:
list_of_values = []
list_of_values.append(value)
current_class_model[feature] = list_of_values
self.classmodels[current_class] = current_class_model
for a_class in self.classmodels.keys():
a_class_model = self.classmodels[a_class]
a_class_model_mean_and_variance = {}
for feature in a_class_model.keys():
a_class_model_mean_and_variance[feature] = findMeanSD(np.array(a_class_model[feature]))
self.classmodelsMeanAndVariance[a_class] = a_class_model_mean_and_variance
def classify(self, testing_vecs):
outputs = []
for vec in testing_vecs:
features = vec[1]
class_model_output_prob = {}
for a_class in self.classmodelsMeanAndVariance.keys():
a_class_output_prob = 0.0
a_class_model_mean_and_variance = self.classmodelsMeanAndVariance[a_class]
for feature_value in features:
feature = feature_value[0]
value = feature_value[1]
#simply ignore a feature if its not seen in training
if(a_class_model_mean_and_variance.has_key(feature)):
feature_mean = a_class_model_mean_and_variance[feature][0]
feature_std = a_class_model_mean_and_variance[feature][1]
a_class_output_prob = a_class_output_prob + math.log10(norm(value,feature_mean,feature_std))
#ignoring P(class) prior.. assuming equal priors
class_model_output_prob[a_class_output_prob] = a_class
probs = class_model_output_prob.keys()
print probs
probs.sort()
max_prob = probs[len(probs)-1]
max_class =class_model_output_prob[max_prob]
outputs.append(max_class)
return outputs
When running on some data, the error I get is
Traceback (most recent call last):
File "C:\Users\Toshiba\workspace\Assignment6\src\gnb_test.py", line 34, in
gaussian = Model.train(testData)
File "C:\Users\Toshiba\workspace\Assignment6\src\gnb.py", line 91, in train
for f in features:
TypeError: 'numpy.float64' object is not iterable
And I dont really [at all] understand what it means
Your traceback suggests that the problem is that you are trying to iterate through features, but features is a float and not a list or tuple - basically, it can't be broken into individual elements. I think it is a float because the lines
for i in trainingdata:
current_class = i[0]
features = i[1]
suggest features keeps getting rewritten as a successive series of numbers, when what you seem to want is to save the numbers into an iterable type. Try
features = []
for i in trainingdata:
current_class = i[0]
features.append(i[1])