Tensorflow core - machine-learning

I run the code below on Tensorflow 1.0 using python 3.5
a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
adder_node = a + b # + provides a shortcut for tf.add(a, b)
sess = tf.Session()
print(sess.run(adder_node, {a: 3, b: 4.5}))
print(sess.run(adder_node, {a: [1, 3], b: [2, 4]}))
I got this error
print(sess.run(adder_node, {a:3, b:4.5}))
Traceback (most recent call last):
File "C:\Users\xxxx\AppData\Local\Continuum\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 1068, in _run
allow_operation=False)
File "C:\Users\xxxx\AppData\Local\Continuum\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\ops.py", line 2708, in as_graph_element
return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
File "C:\Users\xxxx\AppData\Local\Continuum\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\ops.py", line 2787, in _as_graph_element_locked
raise ValueError("Tensor %s is not an element of this graph." % obj)
ValueError: Tensor Tensor("Placeholder_3:0", dtype=float32) is not an element of this graph.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "", line 1, in
File "C:\Users\xxxx\AppData\Local\Continuum\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 895, in run
run_metadata_ptr)
File "C:\Users\xxxx\AppData\Local\Continuum\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 1071, in _run
+ e.args[0])
TypeError: Cannot interpret feed_dict key as Tensor: Tensor Tensor("Placeholder_3:0", dtype=float32) is not an element of this graph.
>
Please help me debug this, not too sure where the error is coming from

Related

Layer model_1 expects 2 input but it received 1 tensors received:[<tf.Tensor IteratorGetNext:0 shape=(None, None, None, None, None) dtype=float32>]

Encountered the error while trying to fit model of encoder-decoder using ConvLSTM2D. the x_train is of shape (31567, 7, 210, 203, 1)(batch_size,framelength,H,W,C).
The encoder part works when executed in isolation but the error occurs when i add the decoder part, seems like the problem is in the input part of decoder but not sure.
tried reshaping the encoder_state_c_1 and encoder_state_h_1 to 5D before passing it to the decoder ConvLSTM2D but doesn't help either.
Please find the code and error here:
MODEL
def define_models_1_moving_1(framelength, n_filter, filter_size):
# define training encoder
encoder_inputs = Input(name = "encoder_input",
shape=(x_train.shape[1], x_train.shape[2], x_train.shape[3], x_train.shape[4]))
encoder_1 = ConvLSTM2D(name = "encoder_ConvLSTM",
filters = n_filter, kernel_size=filter_size, padding='same', return_sequences=True, return_state=True,
kernel_regularizer=l2(0.0005), recurrent_regularizer=l2(0.0005), bias_regularizer=l2(0.0005))
# input_shape=(x_train.shape[1], x_train.shape[2], x_train.shape[3], x_train.shape[4]))
encoder_outputs_1, encoder_state_h_1, encoder_state_c_1 = encoder_1(encoder_inputs)
# define training decoder
decoder_inputs = Input(name = "decoder_input",
shape=(x_train.shape[1], x_train.shape[2], x_train.shape[3], x_train.shape[4]))
decoder_1 = ConvLSTM2D(name = "decoder_ConvLSTM",
filters=n_filter, kernel_size=filter_size, padding='same', return_sequences=True, return_state=True,
kernel_regularizer=l2(0.0005), recurrent_regularizer=l2(0.0005), bias_regularizer=l2(0.0005))
decoder_outputs_1, _, _ = decoder_1([decoder_inputs, encoder_state_h_1, encoder_state_c_1]) #### This line is giving Error
model = Model([encoder_inputs, decoder_inputs], decoder_outputs_1)
return model
Error
Traceback (most recent call last):
File "D:\Chintan\Dataset\model.py", line 155, in
training_history = train_1_moving_1.fit(
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\Admin\AppData\Local\Temp_autograph_generated_filernuwcygs.py", line 15, in tf__train_function
retval = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
ValueError: in user code:
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py", line 1249, in train_function *
return step_function(self, iterator)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py", line 1233, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py", line 1222, in run_step **
outputs = model.train_step(data)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py", line 1023, in train_step
y_pred = self(x, training=True)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\input_spec.py", line 216, in assert_input_compatibility
raise ValueError(
ValueError: Layer "model_120" expects 2 input(s), but it received 1 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(None, None, None, None, None) dtype=float32>]

Tensorboard cannot write data on pytorch: FileNotFoundError: [Errno 2] No such file or directory

I would like to log some data using tensorboard on my pytorch script. This is the structure of my program:
from torch.utils.tensorboard import SummaryWriter
#[methods...]
def main():
device = "cuda" if torch.cuda.is_available() else "cpu"
writer = SummaryWriter("runs/last_train")
#[some code to init the model ...]
for t in range(epochs):
print(f"Epoch {t+1}\n-------------------------------")
train_loss, train_acc = train_loop(train_dataloader, model, loss_fn, optimizer)
val_loss, val_acc = test_loop(val_dataloader, model, loss_fn)
writer.add_scalar('Loss/train', train_loss, t)
writer.add_scalar('Loss/val', val_loss, t)
writer.add_scalar('Accuracy/train', train_acc, t)
writer.add_scalar('Accuracy/val', val_acc, t)
writer.flush()
print("Done!")
writer.close()
if __name__ == "__main__":
main()
The code seems to stop at writer.flush(), with this error:
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
self.run()
File "/home/iaslab/.local/lib/python3.8/site-packages/tensorboard/summary/writer/event_file_writer.py", line 233, in run
self._record_writer.write(data)
File "/home/iaslab/.local/lib/python3.8/site-packages/tensorboard/summary/writer/record_writer.py", line 40, in write
self._writer.write(header + header_crc + data + footer_crc)
File "/home/iaslab/.local/lib/python3.8/site-packages/tensorboard/compat/tensorflow_stub/io/gfile.py", line 766, in write
self.fs.append(self.filename, file_content, self.binary_mode)
File "/home/iaslab/.local/lib/python3.8/site-packages/tensorboard/compat/tensorflow_stub/io/gfile.py", line 160, in append
self._write(filename, file_content, "ab" if binary_mode else "a")
File "/home/iaslab/.local/lib/python3.8/site-packages/tensorboard/compat/tensorflow_stub/io/gfile.py", line 164, in _write
with io.open(filename, mode, encoding=encoding) as f:
FileNotFoundError: [Errno 2] No such file or directory: b'runs/last_train/events.out.tfevents.1655714469.iaslab-Dell-G15-Special-Edition-5521.10389.0'
The program now is stuck. Running CTRL+C, I got:
Traceback (most recent call last):
File "trainMLP.py", line 99, in <module>
main()
File "trainMLP.py", line 92, in main
writer.flush()
File "/home/iaslab/.local/lib/python3.8/site-packages/torch/utils/tensorboard/writer.py", line 1039, in flush
writer.flush()
File "/home/iaslab/.local/lib/python3.8/site-packages/torch/utils/tensorboard/writer.py", line 132, in flush
self.event_writer.flush()
File "/home/iaslab/.local/lib/python3.8/site-packages/tensorboard/summary/writer/event_file_writer.py", line 121, in flush
self._async_writer.flush()
File "/home/iaslab/.local/lib/python3.8/site-packages/tensorboard/summary/writer/event_file_writer.py", line 176, in flush
self._byte_queue.join()
File "/usr/lib/python3.8/queue.py", line 89, in join
self.all_tasks_done.wait()
File "/usr/lib/python3.8/threading.py", line 302, in wait
waiter.acquire()
KeyboardInterrupt
What is the problem? The file actually exists, I have checked.
Thank you in advance!

Where is this dimension error getting generated in my ADAM algorithm?

I am working on main.py in this BRATS Unet
https://github.com/pykao/Modified-3D-UNet-Pytorch/blob/master/main.py
# create your optimizer
print ("Creating Optimizer")
##optimizer = optim.adam(net.parameteres(), lr=)
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)
print ("Created! \n")
trainloader = torch.utils.data.DataLoader(train_idx, batch_size=2, shuffle=True)
testloader = torch.utils.data.DataLoader(test_idx, batch_size=2, shuffle=False)
for epoch in range(2): # loop over the dataset multiple times
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
print("inside for")
# get the inputs THIS ERRORS OUT
inputs, labels = data
# zero the parameter gradients
optimizer.zero_grad()
# forward + backward + optimize
outputs = model(inputs)
loss = criterion(outputs, target)
loss.backward()
optimizer.step()
# print statistics
running_loss += loss.item()
if i % 2000 == 1999: # print every 2000 mini-batches
print('[%d, %5d] loss: %.3f' %
(epoch + 1, i + 1, running_loss / 2000))
running_loss = 0.0
print('Finished Training')
I get this output:
Creating Optimizer
Created!
inside for
Traceback (most recent call last):
File "main.py", line 109, in <module>
outputs = model(inputs)
File "/home/MAHEUNIX/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 489, in __call__
result = self.forward(*input, **kwargs)
File "/mnt/c/Users/MAHE/Modified Unet3D Master -TestRun/model.py", line 99, in forward
out = self.conv3d_c1_1(x)
File "/home/MAHEUNIX/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 489, in __call__
result = self.forward(*input, **kwargs)
File "/home/MAHEUNIX/anaconda3/lib/python3.6/site-packages/torch/nn/modules/conv.py", line 448, in forward
self.padding, self.dilation, self.groups)
RuntimeError: Expected 5-dimensional input for 5-dimensional weight [16, 4, 3, 3, 3], but got 0-dimensional input of size [] instead
I am unfamiliar with PyTorch, and so trainloader, testloader are probably incorrectly used. Please assume I don't know much while you help me. Thanks.
New error:
Traceback (most recent call last):
File "/mnt/c/Users/MAHE/Modified Unet3D Master -TestRun/main.py", line 91, in <module>
for id, info in enumerate(trainloader,0):
File "/home/MAHEUNIX/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 615, in __next__
batch = self.collate_fn([self.dataset[i] for i in indices])
File "/home/MAHEUNIX/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 615, in <listcomp>
batch = self.collate_fn([self.dataset[i] for i in indices])
KeyError: 0
You should pass the dataset to the data loader API. So, pass train_data and test_data instead of train_idx and test_idx to torch.utils.data.DataLoader.

Tensorflow function: dilation2d() raises shape error

I want to setup a convolutional neuronal net in tensorflow r1.2 with following structure:
def construct_nn2(input_layer, net2={}):
input_layer1 = tf.nn.dilation2d(
input=input_layer,
strides=[1, 1, 1, 1],
rates=[1, 1, 1, 1],
filter=[1.0, 1.0, 1],
padding='SAME',
name='dil'
)
net2['conv11'] = tf.layers.conv2d(
inputs=input_layer1,
filters=64,
kernel_size=[5, 5],
padding='same',
activation=tf.tanh,
name='conv11'
)
net2['conv12'] = tf.layers.conv2d(
inputs=net2['conv11'],
filters=64,
kernel_size=[3, 3],
padding='same',
activation=tf.tanh,
name='conv12'
)
net2['logits']= tf.layers.conv2d(
inputs=net2['conv12'],
filters=1,
kernel_size=[3, 3],
padding='same',
activation=tf.sigmoid,
name='logits'
)
return net2['logits']
The dilation layer spits out these errors:
Traceback (most recent call last):
File "/home/test/Dropbox/occlusion_thesis/occ_small _2_add/main.py", line 137, in <module>
tn_prediction = construct_nn2(t_img)
File "/home/test/Dropbox/occlusion_thesis/occ_small _2_add/main.py", line 18, in construct_nn2
name='dil'
File "/home/test/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/gen_nn_ops.py", line 860, in dilation2d
name=name)
File "/home/test/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
op_def=op_def)
File "/home/test/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2508, in create_op
set_shapes_for_outputs(ret)
File "/home/test/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1873, in set_shapes_for_outputs
shapes = shape_func(op)
File "/home/test/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1823, in call_with_requiring
return call_cpp_shape_fn(op, require_shape_fn=True)
File "/home/test/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 610, in call_cpp_shape_fn
debug_python_shape_fn, require_shape_fn)
File "/home/test/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 676, in _call_cpp_shape_fn_impl
raise ValueError(err.message)
ValueError: Shape must be rank 3 but is rank 1 for 'dil' (op: 'Dilation2D') with input shapes: [1,248,360,128], [3].
What I have to change that the errors do not raise. I have read the dilation2d, but still I do not have any clue, what I have to change that may get this layer work at least without errors.
Edit:
The filter must be a tensor with the same shape.
input_layer1 = tf.nn.dilation2d(
input=input_layer,
strides=[1, 1, 1, 1],
rates=[1, 1, 1, 1],
filter=tf.squeeze(input_layer, axis=0),
padding='SAME',
name='dil'
)
Refactoring the filter does run through at least.

Keras Wrappers for Scikit Learn - AUC scorer is not working

I'm trying to use Keras Scikit Learn Wrapper in order to make random search for parameters easier. I wrote an example code here where :
I generate an artificial dataset:
I am using moons from scikit learn
from sklearn.datasets import make_moons
dataset = make_moons(1000)
Model builder definition:
I define build_fn function needed:
def build_fn(nr_of_layers = 2,
first_layer_size = 10,
layers_slope_coeff = 0.8,
dropout = 0.5,
activation = "relu",
weight_l2 = 0.01,
act_l2 = 0.01,
input_dim = 2):
result_model = Sequential()
result_model.add(Dense(first_layer_size,
input_dim = input_dim,
activation=activation,
W_regularizer= l2(weight_l2),
activity_regularizer=activity_l2(act_l2)
))
current_layer_size = int(first_layer_size * layers_slope_coeff) + 1
for index_of_layer in range(nr_of_layers - 1):
result_model.add(BatchNormalization())
result_model.add(Dropout(dropout))
result_model.add(Dense(current_layer_size,
W_regularizer= l2(weight_l2),
activation=activation,
activity_regularizer=activity_l2(act_l2)
))
current_layer_size = int(current_layer_size * layers_slope_coeff) + 1
result_model.add(Dense(1,
activation = "sigmoid",
W_regularizer = l2(weight_l2)))
result_model.compile(optimizer="rmsprop", metrics = ["accuracy"], loss = "binary_crossentropy")
return result_model
NeuralNet = KerasClassifier(build_fn)
Parameter grid definition :
Then I defined a parameter grid :
param_grid = {
"nr_of_layers" : [2, 3, 4, 5],
"first_layer_size" : [5, 10, 15],
"layers_slope_coeff" : [0.4, 0.6, 0.8],
"dropout" : [0.3, 0.5, 0.8],
"weight_l2" : [0.01, 0.001, 0.0001],
"verbose" : [0],
"batch_size" : [1],
"nb_epoch" : [30]
}
RandomizedSearchCV phase:
I defined RandomizedSearchCV object and fitted with values from artificial dataset:
random_search = RandomizedSearchCV(NeuralNet,
param_distributions=param_grid, verbose=2, n_iter=1, scoring="roc_auc")
random_search.fit(dataset[0], dataset[1])
What I got (after running this code in console) is :
Traceback (most recent call last):
File "C:\Anaconda2\lib\site-packages\IPython\core\interactiveshell.py", line 2885, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-3-c5bdbc2770b7>", line 2, in <module>
random_search.fit(dataset[0], dataset[1])
File "C:\Anaconda2\lib\site-packages\sklearn\grid_search.py", line 996, in fit
return self._fit(X, y, sampled_params)
File "C:\Anaconda2\lib\site-packages\sklearn\grid_search.py", line 553, in _fit
for parameters in parameter_iterable
File "C:\Anaconda2\lib\site-packages\sklearn\externals\joblib\parallel.py", line 800, in __call__
while self.dispatch_one_batch(iterator):
File "C:\Anaconda2\lib\site-packages\sklearn\externals\joblib\parallel.py", line 658, in dispatch_one_batch
self._dispatch(tasks)
File "C:\Anaconda2\lib\site-packages\sklearn\externals\joblib\parallel.py", line 566, in _dispatch
job = ImmediateComputeBatch(batch)
File "C:\Anaconda2\lib\site-packages\sklearn\externals\joblib\parallel.py", line 180, in __init__
self.results = batch()
File "C:\Anaconda2\lib\site-packages\sklearn\externals\joblib\parallel.py", line 72, in __call__
return [func(*args, **kwargs) for func, args, kwargs in self.items]
File "C:\Anaconda2\lib\site-packages\sklearn\cross_validation.py", line 1550, in _fit_and_score
test_score = _score(estimator, X_test, y_test, scorer)
File "C:\Anaconda2\lib\site-packages\sklearn\cross_validation.py", line 1606, in _score
score = scorer(estimator, X_test, y_test)
File "C:\Anaconda2\lib\site-packages\sklearn\metrics\scorer.py", line 175, in __call__
y_pred = y_pred[:, 1]
IndexError: index 1 is out of bounds for axis 1 with size 1
This code work fine when instead of using scoring = "roc_auc" I used accuracy metric. Can anyone explain me what's wrong? Have anyone had similiar problem?
There is a bug in the KerasClassifier that is causing this issue. I have opened an issue for it on the repo. https://github.com/fchollet/keras/issues/2864
The fix is also in there. You can define your own KerasClassifier in the mean time as a temporary workaround.
class FixedKerasClassifier(KerasClassifier):
def predict_proba(self, X, **kwargs):
kwargs = self.filter_sk_params(Sequential.predict_proba, kwargs)
probs = self.model.predict_proba(X, **kwargs)
if(probs.shape[1] == 1):
probs = np.hstack([1-probs,probs])
return probs

Resources