zoukankan      html  css  js  c++  java
  • tensorflow-解决3个问题

    问题一:对特征归一化

    Min-Max Scaling:
    X′=a+(X−Xmin)(b−a)/(Xmax−Xmin)
    
    # Problem 1 - Implement Min-Max scaling for grayscale image data
    def normalize_grayscale(image_data):
        """
        Normalize the image data with Min-Max scaling to a range of [0.1, 0.9]
        :param image_data: The image data to be normalized
        :return: Normalized image data
        """
        # TODO: Implement Min-Max scaling for grayscale image data
        a = 0.1
        b = 0.9
        grayscal_min=0
        grayscal_max=255
        return a + (((image_data-grayscal_min)*(b-a))/(grayscal_max-grayscal_min))
    

    问题二:用 TensorFlow 创建特征、目标、权重和偏置项 tensor。

    # All the pixels in the image (28 * 28 = 784)
    features_count = 784
    # All the labels
    labels_count = 10
    
    # TODO: Set the features and labels tensors
    features = tf.placeholder(tf.float32)
    labels = tf.placeholder(tf.float32)
    
    # TODO: Set the weights and biases tensors
    weights = tf.Variable(tf.truncated_normal((features_count,labels_count)))
    
    biases = tf.Variable(tf.zeros(labels_count))
    
    
    ### DON'T MODIFY ANYTHING BELOW ###
    
    #Test Cases
    from tensorflow.python.ops.variables import Variable
    
    assert features._op.name.startswith('Placeholder'), 'features must be a placeholder'
    assert labels._op.name.startswith('Placeholder'), 'labels must be a placeholder'
    assert isinstance(weights, Variable), 'weights must be a TensorFlow variable'
    assert isinstance(biases, Variable), 'biases must be a TensorFlow variable'
    
    assert features._shape == None or (
        features._shape.dims[0].value is None and
        features._shape.dims[1].value in [None, 784]), 'The shape of features is incorrect'
    assert labels._shape  == None or (
        labels._shape.dims[0].value is None and
        labels._shape.dims[1].value in [None, 10]), 'The shape of labels is incorrect'
    assert weights._variable._shape == (784, 10), 'The shape of weights is incorrect'
    assert biases._variable._shape == (10), 'The shape of biases is incorrect'
    
    assert features._dtype == tf.float32, 'features must be type float32'
    assert labels._dtype == tf.float32, 'labels must be type float32'
    
    # Feed dicts for training, validation, and test session
    train_feed_dict = {features: train_features, labels: train_labels}
    valid_feed_dict = {features: valid_features, labels: valid_labels}
    test_feed_dict = {features: test_features, labels: test_labels}
    
    # Linear Function WX + b
    logits = tf.matmul(features, weights) + biases
    
    prediction = tf.nn.softmax(logits)
    
    # Cross entropy
    cross_entropy = -tf.reduce_sum(labels * tf.log(prediction), reduction_indices=1)
    
    # Training loss
    loss = tf.reduce_mean(cross_entropy)
    
    # Create an operation that initializes all variables
    init = tf.global_variables_initializer()
    
    # Test Cases
    with tf.Session() as session:
        session.run(init)
        session.run(loss, feed_dict=train_feed_dict)
        session.run(loss, feed_dict=valid_feed_dict)
        session.run(loss, feed_dict=test_feed_dict)
        biases_data = session.run(biases)
    
    assert not np.count_nonzero(biases_data), 'biases must be zeros'
    
    print('Tests Passed!')
    

    问题三:调整学习率,epochs 和 batch size 来获取最高准确率

  • 相关阅读:
    7.1MongoDB之索引
    7.1MongoDB之排序
    6.30MongoDB之Limit与Skip方法
    6.30Java连接MongoDB进行操作练习
    6.30MongoDB之$type操作符
    6.30MongoDB之条件操作符
    6.30MongoDB之"查"
    6.29MongoDB之"改"
    c# 调用jar包
    sql server SQL 调试:无法启动 T-SQL 调试。未能附加到 SQL Server 进程
  • 原文地址:https://www.cnblogs.com/james0/p/8465196.html
Copyright © 2011-2022 走看看