Torch.Min in tensorflow - tensorflow

In torch there is a module named, torch.Min, that can apply the min operation over different branches of a network.
nn.Min
is there something similar in tensorflow?. (noob in tensorflow)

I think you want tf.reduce_min. To find the min of [10, 3, 5, 4]:
import tensorflow as tf
def one_dim_graph():
input_tensor = tf.constant([10, 3, 5, 4])
tensor_min = tf.reduce_min(input_tensor)
return input_tensor, tensor_min
def run():
in_one, g_one = one_dim_graph()
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
out_one = sess.run([g_one])
print in_one
print out_one
run()
This will give you the min:
Tensor("Const_74:0", shape=(4,), dtype=int32)
[3]
You can also use this across multi-dimension tensors and reduce all dimensions or reduce along a certain dimension. So, for example, to find the min of:
[[1, 4, 3, 10],
[6, 5, 2, 12],
[9, 7, 8, 11]]
We can find:
the total min, [1] with tf.reduce_min(input_tensor)
the column-wise min [1, 4, 2, 10] with tf.reduce_min(input_tensor, reduction_indices=0)
the row-wise min [1, 2, 7] with tf.reduce_min(input_tensor, reduction_indices=1)

Related

Element-wise assignment in tensorflow

In numpy, it could be easily done as
>>> img
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]], dtype=int32)
>>> img[img>5] = [1,2,3,4]
>>> img
array([[1, 2, 3],
[4, 5, 1],
[2, 3, 4]], dtype=int32)
However, there seems not exist similar operation in tensorflow.
You can never assign a value to a tensor in tensorflow as the change in tensor value is not traceable by backpropagation, but you can still get another tensor from origin tensor, here is a solution
import tensorflow as tf
tf.enable_eager_execution()
img = tf.constant(list(range(1, 10)), shape=[3, 3])
replace_mask = img > 5
keep_mask = tf.logical_not(replace_mask)
keep = tf.boolean_mask(img, keep_mask)
keep_index = tf.where(keep_mask)
replace_index = tf.where(replace_mask)
replace = tf.random_uniform((tf.shape(replace_index)[0],), 0, 10, tf.int32)
updates = tf.concat([keep, replace], axis=0)
indices = tf.concat([keep_index, replace_index], axis=0)
result = tf.scatter_nd(tf.cast(indices, tf.int32), updates, shape=tf.shape(img))
Actually there is a way to achieve this. Very similar to #Jie.Zhou's answer, you can replace tf.constant with tf.Variable, then replace tf.scatter_nd with tf.scatter_nd_update

what TensorFlow hash_bucket_size matters

I am creating a DNNclassifier with sparse columns. The training data looks like this,
samples col1 col2 price label
eg1 [[0,1,0,0,0,2,0,1,0,3,...] [[0,0,4,5,0,...] 5.2 0
eg2 [0,0,...] [0,0,...] 0 1
eg3 [0,0,...]] [0,0,...] 0 1
The following snippet can run successfully,
import tensorflow as tf
sparse_feature_a = tf.contrib.layers.sparse_column_with_hash_bucket('col1', 3, dtype=tf.int32)
sparse_feature_b = tf.contrib.layers.sparse_column_with_hash_bucket('col2', 1000, dtype=tf.int32)
sparse_feature_a_emb = tf.contrib.layers.embedding_column(sparse_id_column=sparse_feature_a, dimension=2)
sparse_feature_b_emb = tf.contrib.layers.embedding_column(sparse_id_column=sparse_feature_b, dimension=2)
feature_c = tf.contrib.layers.real_valued_column('price')
estimator = tf.contrib.learn.DNNClassifier(
feature_columns=[sparse_feature_a_emb, sparse_feature_b_emb, feature_c],
hidden_units=[5, 3],
n_classes=2,
model_dir='./tfTmp/tfTmp0')
# Input builders
def input_fn_train(): # returns x, y (where y represents label's class index).
features = {'col1': tf.SparseTensor(indices=[[0, 1], [0, 5], [0, 7], [0, 9]],
values=[1, 2, 1, 3],
dense_shape=[3, int(250e6)]),
'col2': tf.SparseTensor(indices=[[0, 2], [0, 3]],
values=[4, 5],
dense_shape=[3, int(100e6)]),
'price': tf.constant([5.2, 0, 0])}
labels = tf.constant([0, 1, 1])
return features, labels
estimator.fit(input_fn=input_fn_train, steps=100)
However, I have a question from this sentence,
sparse_feature_a = tf.contrib.layers.sparse_column_with_hash_bucket('col1', 3, dtype=tf.int32)
where 3 means hash_bucket_size=3, but this sparse tensor includes 4 non-zero values,
'col1': tf.SparseTensor(indices=[[0, 1], [0, 5], [0, 7], [0, 9]],
values=[1, 2, 1, 3],
dense_shape=[3, int(250e6)])
It seems has_bucket_size does nothing here. No matter how many non-zero values you have in your sparse tensor, you just need to set it with an integer > 1 and it works correctly.
I know my understanding may not be right. Could anyone explain how has_bucket_size works? Thanks a lot!
hash_bucket_size works by taking the original indices, hashing them into a space of the specified size, and using the hashed indices as features.
This means you can specify your model before knowing the full range of possible indices, at the cost of some indices maybe colliding.

Normalize numpy ndarray data

My data is numpy ndarray with shape(2,3,4) following this:
I've try to normalize 0-1 scale for each column through sklearn normalization.
from sklearn.preprocessing import normalize
x = np.array([[[1, 2, 3, 4],
[2, 2, 3, 4],
[3, 2, 3, 4]],
[[4, 2, 3, 4],
[5, 2, 3, 4],
[6, 2, 3, 4]]])
x.shape ==> ( 2,3,4)
x = normalize(x, norm='max', axis=0, )
However, I catch the error :
ValueError: Found array with dim 3. the normalize function expected <= 2.
How do I solve this problem?
Thank you.
It seems scikit-learn expects ndarrays with at most two dims. So, to solve it would be to reshape to 2D, feed it to normalize that gives us a 2D array, which could be reshaped back to original shape -
from sklearn.preprocessing import normalize
normalize(x.reshape(x.shape[0],-1), norm='max', axis=0).reshape(x.shape)
Alternatively, it's much simpler with NumPy that works fine with generic ndarrays -
x/np.linalg.norm(x, ord=np.inf, axis=0, keepdims=True)

Can tensorflow handle categorical features with multiple inputs within one column?

For example, I have data in the following csv format:
1, 2, 1:3:4, 2
0, 1, 3:5, 1
...
Each column seperated by comma represent one feature. Normally, a feature is one-hot(e.g. col0, col1, col3), but in this case, the feature for col2 has multiple inputs(seperated by colon).
I'm sure tensorflow can handle one-hot feature with sparse tensor, but I'm not sure if it could handle feature with multiple inputs like col2?
And if ok, how should it be represented in tensorflow's sparse tensor?
TensorFlow has some string processing ops which can handle lists within CSVs. I'd read the list as a string column first, the process it like this:
def process_list_column(list_column, dtype=tf.float32):
sparse_strings = tf.string_split(list_column, delimiter=":")
return tf.SparseTensor(indices=sparse_strings.indices,
values=tf.string_to_number(sparse_strings.values,
out_type=dtype),
dense_shape=sparse_strings.dense_shape)
An example of using this function:
# csv_input.csv contains:
# 1,2,1:3:4,2
# 0,1,3:5,1
filename_queue = tf.train.string_input_producer(["csv_input.csv"])
# Read two lines, batched
_, lines = tf.TextLineReader().read_up_to(filename_queue, 2)
columns = tf.decode_csv(lines, record_defaults=[[0], [0], [""], [0]])
columns[2] = process_list_column(columns[2], dtype=tf.int32)
with tf.Session() as session:
coordinator = tf.train.Coordinator()
tf.train.start_queue_runners(session, coord=coordinator)
print(session.run(columns))
coordinator.request_stop()
coordinator.join()
Outputs:
[array([1, 0], dtype=int32),
array([2, 1], dtype=int32),
SparseTensorValue(indices=array([[0, 0],
[0, 1],
[0, 2],
[1, 0],
[1, 1]]),
values=array([1, 3, 4, 3, 5], dtype=int32),
dense_shape=array([2, 3])),
array([2, 1], dtype=int32)]

How to use tensorflow to implement deconvolution?

I want to take use of tensorflow to implement fully convolutional network. There is a function
tf.nn.conv2d_transpose(value, filter, output_shape, strides, padding, name),
which could be used to take bilinear upsampling. However, I am confused as to how to use it? The input is an image with a single channel, and the output is also an image with a single channel, whose size is two times the one of the input.
I tried to use the function as follows, but got an IndexError: list index out of range:
with tf.name_scope('deconv') as scope:
deconv = tf.nn.conv2d_transpose(conv6, [3, 3, 1, 1],
[1, 26, 20, 1], 2, padding='SAME', name=None)
Got it! (assuming input_size = [1, 13, 10,1])
with tf.name_scope('deconv') as scope:
deconv = tf.nn.conv2d_transpose(input_layer, [3, 3, 1, 1],
[1, 26, 20, 1], [1, 2, 2, 1], padding='SAME', name=None)