fork download
  1. import tensorflow as tf
  2. import numpy as np
  3.  
  4. # Create 100 phony x, y data points in NumPy, y = x * 0.1 + 0.3
  5. x_data = np.random.rand(100).astype(np.float32)
  6. y_data = x_data * 0.1 + 0.3
  7.  
  8. # Try to find values for W and b that compute y_data = W * x_data + b
  9. # (We know that W should be 0.1 and b 0.3, but TensorFlow will
  10. # figure that out for us.)
  11. W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
  12. b = tf.Variable(tf.zeros([1]))
  13. y = W * x_data + b
  14.  
  15. # Minimize the mean squared errors.
  16. loss = tf.reduce_mean(tf.square(y - y_data))
  17. optimizer = tf.train.GradientDescentOptimizer(0.5)
  18. train = optimizer.minimize(loss)
  19.  
  20. # Before starting, initialize the variables. We will 'run' this first.
  21. init = tf.initialize_all_variables()
  22.  
  23. # Launch the graph.
  24. sess = tf.Session()
  25. sess.run(init)
  26.  
  27. # Fit the line.
  28. for step in range(201):
  29. sess.run(train)
  30. if step % 20 == 0:
  31. print(step, sess.run(W), sess.run(b))
  32.  
  33. # Learns best fit is W: [0.1], b: [0.3]# your code goes here
Success #stdin #stdout #stderr 1.27s 203720KB
stdin
To solve the XOR problem using a Deep Neural Network (DNN) in Python, you can use libraries like TensorFlow or PyTorch. I'll provide an example using TensorFlow:

```python
import tensorflow as tf
import numpy as np

# Define the XOR dataset
x_data = np.array([[0, 0], [0, 1], [1, 0], [1, 1],], dtype=np.float32)
y_data = np.array([[0], [1], [1], [0],], dtype=np.float32)

# Define the DNN model
model = tf.keras.models.Sequential([
    tf.keras.layers.Input(shape=(2,)),
    tf.keras.layers.Dense(8, activation='relu'),
    tf.keras.layers.Dense(1, activation='sigmoid')
])

# Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# Train the model
model.fit(x_data, y_data, epochs=10000, verbose=0)

# Evaluate the model
loss, accuracy = model.evaluate(x_data, y_data)
print(f'Loss: {loss}, Accuracy: {accuracy}')

# Make predictions
predictions = model.predict(x_data)
print("Predictions:")
print(predictions)
```

In this example, we first define the XOR dataset, create a DNN model with an input layer, a hidden layer with ReLU activation, and an output layer with sigmoid activation. We compile the model using binary cross-entropy loss and the Adam optimizer. Then, we train the model for a large number of epochs and evaluate its performance. Finally, we make predictions using the trained model.

Make sure you have TensorFlow installed before running this code. You can install it with `pip install tensorflow`.
stdout
(0, array([0.6921862], dtype=float32), array([-0.04412946], dtype=float32))
(20, array([0.21779542], dtype=float32), array([0.23556986], dtype=float32))
(40, array([0.1230993], dtype=float32), array([0.28736547], dtype=float32))
(60, array([0.10452971], dtype=float32), array([0.29752243], dtype=float32))
(80, array([0.10088827], dtype=float32), array([0.29951417], dtype=float32))
(100, array([0.10017419], dtype=float32), array([0.29990473], dtype=float32))
(120, array([0.10003416], dtype=float32), array([0.29998133], dtype=float32))
(140, array([0.10000671], dtype=float32), array([0.29999635], dtype=float32))
(160, array([0.10000133], dtype=float32), array([0.2999993], dtype=float32))
(180, array([0.10000026], dtype=float32), array([0.29999986], dtype=float32))
(200, array([0.10000005], dtype=float32), array([0.29999998], dtype=float32))
stderr
WARNING:tensorflow:From /usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
WARNING:tensorflow:From /usr/local/lib/python2.7/dist-packages/tensorflow/python/util/tf_should_use.py:193: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.
Instructions for updating:
Use `tf.global_variables_initializer` instead.