我知道tf.placeholder的基本用法:
x = tf.placeholder(tf.float32, shape=(1024, 1024))
y = tf.matmul(x, x)
with tf.Session() as sess:
print(sess.run(y)) # ERROR: will fail because x was not fed.
rand_array = np.random.rand(1024, 1024)
print(sess.run(y, feed_dict={x: rand_array})) # Will succeed.
Run Code Online (Sandbox Code Playgroud)
我知道第二个参数是关于形状的.但是当形状中第一个是None时,我不知道这是什么意思.例如:[无,784].
这是用于测试的代码:
import numpy as np # maybe you should download the package
import pandas as pd # maybe you should download the package
data = ['Romance|Fantasy|Family|Drama', 'War|Adventure|Science Fiction',
'Action|Family|Science Fiction|Adventure|Mystery', 'Action|Drama',
'Action|Drama|Thriller', 'Drama|Romance', 'Comedy|Drama', 'Action',
'Comedy', 'Crime|Comedy|Action|Adventure',
'Drama|Thriller|History', 'Action|Science Fiction|Thriller']
a = pd.Series(data)
print(a.str.contains("|"))
print(a.apply(lambda x:"|" in x))
print(a)
Run Code Online (Sandbox Code Playgroud)
执行上面的代码后,您将获得以下三个输出:
0 True
1 True
2 True
3 True
4 True
5 True
6 True
7 True
8 True
9 True
10 True
11 True
dtype: bool
Run Code Online (Sandbox Code Playgroud)
print(a.apply(lambda x:"|" in x)) …
我正在做cs224n 的赋值。在函数中test_word2vec,有一些我不明白的 python 语法:
""" Interface to the dataset for negative sampling """
dataset = type('dummy', (), {})()
def dummySampleTokenIdx():
return random.randint(0, 4)
def getRandomContext(C):
tokens = ["a", "b", "c", "d", "e"]
return tokens[random.randint(0,4)], \
[tokens[random.randint(0,4)] for i in xrange(2*C)]
dataset.sampleTokenIdx = dummySampleTokenIdx
dataset.getRandomContext = getRandomContext
Run Code Online (Sandbox Code Playgroud)
问题一:什么dataset = type('dummy', (), {})()意思?
问题二:在dataset.sampleTokenIdx = dummySampleTokenIdx,我认为dataset没有属性sampleTokenIdx。那么,为什么数据集可以调用它呢?
功能keras.metrics.binary_accuracy非常简单:
def binary_accuracy(y_true, y_pred):
return K.mean(K.equal(y_true, K.round(y_pred)), axis=-1)
Run Code Online (Sandbox Code Playgroud)
https://github.com/fchollet/keras/blob/master/keras/metrics.py#L20
但是功能keras.metrics.categorical_accuracy有所不同:
def categorical_accuracy(y_true, y_pred):
return K.cast(K.equal(K.argmax(y_true, axis=-1),
K.argmax(y_pred, axis=-1)),
K.floatx())
Run Code Online (Sandbox Code Playgroud)
https://github.com/fchollet/keras/blob/master/keras/metrics.py#L24
我很困惑,为什么这个功能使用K.cast,而不是K.mean?因为我认为这个函数应该像函数一样返回一个数字keras.metrics.binary_accuracy